Skip to content

Fix: normalize nullsNotDistinct to false in pgSerializer to stop spurious truncate prompts#5989

Open
tsushanth wants to merge 1 commit into
drizzle-team:mainfrom
tsushanth:fix/5955-unique-nullsnotdistinct-undefined
Open

Fix: normalize nullsNotDistinct to false in pgSerializer to stop spurious truncate prompts#5989
tsushanth wants to merge 1 commit into
drizzle-team:mainfrom
tsushanth:fix/5955-unique-nullsnotdistinct-undefined

Conversation

@tsushanth

Copy link
Copy Markdown

Fixes #5955

Problem

drizzle:push repeatedly prompts to truncate a table for a unique() constraint that already exists in the database and hasn't changed.

Root cause: nullsNotDistinct mismatch between the two snapshot sources fed into the differ:

  • TypeScript schema serialization (pgSerializer.ts line 320): stores unq.nullsNotDistinct verbatim. A plain unique() without .nullsNotDistinct() leaves this field as undefined.
  • DB introspection (pgSerializer.ts line 1357): always stores false for existing UNIQUE constraints.

PgSquasher.squashUnique serializes the field directly — undefined and false stringify differently, so the differ sees the constraint as altered on every run. The diff engine turns an altered unique constraint into drop + re-add, which requires a truncate.

Fix

One line in drizzle-kit/src/serializer/pgSerializer.ts:

- nullsNotDistinct: unq.nullsNotDistinct,
+ nullsNotDistinct: unq.nullsNotDistinct ?? false,

This aligns the schema-side snapshot with what the DB introspector produces for the same constraint, so the differ sees no change.

When a unique() constraint is defined without .nullsNotDistinct(), the
ORM sets nullsNotDistinct to undefined. The DB introspector always
returns false for this field. PgSquasher.squashUnique serializes the
field as-is, producing different squashed strings for the same
constraint:
  schema: ...;undefined
  db:     ...;false

The differ sees this as an altered constraint, which the push logic
turns into a delete + re-add, triggering the truncate prompt on every
push run.

Normalizing undefined to false at serialization time aligns the two
snapshot sources and stops the spurious re-add cycle.

Fixes drizzle-team#5955
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.

[BUG]: drizzle:push repeatedly prompts to truncate table for unique() constraint that already exists in the database

2 participants