Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skip virtual fields on UPSERTs #405

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,16 @@ class CQN2SQLRenderer {
* @returns {string} SQL
*/
UPSERT(q) {
let { UPSERT } = q,
sql = this.INSERT({ __proto__: q, INSERT: UPSERT })
const { UPSERT } = q
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The q.target topic is already being addressed so this comment could be removed.

Suggested change
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 => !keys.includes(c))
.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)}`)

// temporal data
Expand Down
Loading