fix(postgres): handle empty SET clause in upsertOne#924
Open
JacobiusMakes wants to merge 1 commit intolinagora:mainfrom
Open
fix(postgres): handle empty SET clause in upsertOne#924JacobiusMakes wants to merge 1 commit intolinagora:mainfrom
JacobiusMakes wants to merge 1 commit intolinagora:mainfrom
Conversation
When upsertOne receives an entity with only primary key fields (no non-PK fields to update), buildUpdate generates an invalid SQL statement with an empty SET clause: `UPDATE ... SET WHERE ...`. This causes a PostgreSQL syntax error (42601), and the fallback INSERT then fails with a duplicate key violation. Fix: detect when there are no non-PK fields to update and use INSERT ... ON CONFLICT DO NOTHING instead. This correctly handles the SSO case where the external_user_repository row already exists and there is nothing to update beyond the composite primary key. Closes linagora#768 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
upsertOne()generates invalid SQL (UPDATE ... SET WHERE ...) when an entity has only primary key fields and no non-PK fields to updatehasNonPrimaryKeyFields()check to detect the empty-SET case before attempting an UPDATEbuildInsertOnConflictDoNothing()query builder method to safely ensure the row exists without duplicate key violationsRoot Cause
When syncing SSO user data, the
external_user_repositoryentity sometimes has only its composite primary key (service_id,external_id,user_id) set with no additional fields.buildUpdate()produces an empty SET clause, causing PostgreSQL error 42601. The fallbackbuildInsert()then fails with a duplicate key violation since the row already exists.Fix
When no non-PK fields are present, skip the UPDATE entirely and use
INSERT ... ON CONFLICT DO NOTHING. If the row already exists, the INSERT is a no-op and returnstrue. If it doesn't exist, it gets inserted.Closes #768
Test plan
🤖 Generated with Claude Code