Skip to content

Commit

Permalink
fix: Restore former deep upsert behavior / error (#406)
Browse files Browse the repository at this point in the history
Excluding associations from UPSERT broke a test in @sap/cds, so this PR
restores the former behavior for the time being which let updates to
associations slip through and fail with a database error.

We need to follow up on what the correct behavior should be...
  • Loading branch information
danjoa authored Jan 9, 2024
1 parent c1df747 commit 284b1e3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,19 @@ class CQN2SQLRenderer {
const elements = q.target?.elements || {}
let sql = this.INSERT({ __proto__: q, INSERT: UPSERT })
let keys = q.target?.keys
if (!keys) return (this.sql = sql) // REVISIT: We should converge q.target and q._target
if (!keys) return this.sql = sql
keys = Object.keys(keys).filter(k => !keys[k].isAssociation)

let updateColumns = q.UPSERT.entries ? Object.keys(q.UPSERT.entries[0]) : this.columns
updateColumns = updateColumns
.filter(c => c in elements && !elements[c].virtual && !elements[c].value && !elements[c].isAssociation && !keys.includes(c))
.map(c => `${this.quote(c)} = excluded.${this.quote(c)}`)
updateColumns = updateColumns.filter(c => {
if (keys.includes(c)) return false //> keys go into ON CONFLICT clause
let e = elements[c]
if (!e) return true //> pass through to native SQL columns not in CDS model
if (e.virtual) return true //> skip virtual elements
if (e.value) return true //> skip calculated elements
// if (e.isAssociation) return true //> this breaks a a test in @sap/cds -> need to follow up how to correctly handle deep upserts
else return true
}).map(c => `${this.quote(c)} = excluded.${this.quote(c)}`)

// temporal data
keys.push(...Object.values(q.target.elements).filter(e => e['@cds.valid.from']).map(e => e.name))
Expand Down

0 comments on commit 284b1e3

Please sign in to comment.