Skip to content

fix(postgres): handle empty SET clause in upsertOne#924

Open
JacobiusMakes wants to merge 1 commit intolinagora:mainfrom
JacobiusMakes:fix/768-sso-upsert-constraint
Open

fix(postgres): handle empty SET clause in upsertOne#924
JacobiusMakes wants to merge 1 commit intolinagora:mainfrom
JacobiusMakes:fix/768-sso-upsert-constraint

Conversation

@JacobiusMakes
Copy link
Copy Markdown

Summary

  • Fixes the SSO upsert constraint issue where upsertOne() generates invalid SQL (UPDATE ... SET WHERE ...) when an entity has only primary key fields and no non-PK fields to update
  • Adds hasNonPrimaryKeyFields() check to detect the empty-SET case before attempting an UPDATE
  • Adds buildInsertOnConflictDoNothing() query builder method to safely ensure the row exists without duplicate key violations

Root Cause

When syncing SSO user data, the external_user_repository entity 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 fallback buildInsert() 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 returns true. If it doesn't exist, it gets inserted.

Closes #768

Test plan

  • Verify SSO login flow with an existing external user (triggers upsert with PK-only entity)
  • Verify SSO login flow with a new external user (triggers normal insert path)
  • Verify normal entity updates with non-PK fields still work via the UPDATE path

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSO upsert constraint issue

1 participant