Skip to content

Commit

Permalink
fix: flat update with arbitrary where clauses (#598)
Browse files Browse the repository at this point in the history
Flat update with or on keys in where resulted in error
`UNIQUE_CONSTRAINT_VIOLATION`.
Solution: Only enrich data with keys from where for deep update.
  • Loading branch information
larslutz96 authored Apr 18, 2024
1 parent eb959e3 commit f108798
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions db-service/lib/deep-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,5 @@ module.exports = {
onDeep,
getDeepQueries,
getExpandForDeep,
hasDeep,
}
6 changes: 4 additions & 2 deletions db-service/lib/fill-in-keys.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cds = require('@sap/cds')
const { hasDeep } = require('../lib/deep-queries')

// REVISIT: very deep & fragile dependencies to internal modules -> copy these into here
const propagateForeignKeys = require('@sap/cds/libx/_runtime/common/utils/propagateForeignKeys')
Expand Down Expand Up @@ -57,9 +58,10 @@ const generateUUIDandPropagateKeys = (entity, data, event) => {
module.exports = async function fill_in_keys(req, next) {
// REVISIT dummy handler until we have input processing
if (!req.target || !this.model || req.target._unresolved) return next()
if (req.event === 'UPDATE') {
// only for deep update
if (req.event === 'UPDATE' && hasDeep(req.query, req.target)) {
// REVISIT for deep update we need to inject the keys first
enrichDataWithKeysFromWhere (req.data, req, this)
enrichDataWithKeysFromWhere(req.data, req, this)
}

// REVISIT no input processing for INPUT with rows/values
Expand Down
33 changes: 33 additions & 0 deletions test/compliance/UPDATE.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const cds = require('../cds.js')
const Books = 'complex.Books'

describe('UPDATE', () => {
describe('entity', () => {
test.skip('missing', () => {
Expand All @@ -12,6 +15,36 @@ describe('UPDATE', () => {
})

describe('where', () => {
cds.test(__dirname + '/resources')
test('flat with or on key', async () => {
const entires = [
{
ID: 5,
title: 'foo',
},
{
ID: 6,
title: 'bar',
},
]

const insert = await cds.run(INSERT.into(Books).entries(entires))
expect(insert.affectedRows).toEqual(2)

const update = await cds.run(
UPDATE.entity(Books)
.set({
title: 'foo',
})
.where({
ID: 5,
or: {
ID: 6,
},
}),
)
expect(update).toEqual(2)
})
test.skip('missing', () => {
throw new Error('not supported')
})
Expand Down

0 comments on commit f108798

Please sign in to comment.