Skip to content

Make PostgreSQL foreign key drops idempotent#27

Merged
ksimbili merged 2 commits into
mainfrom
fix/idempotent-pg-foreign-key-drop
Jul 17, 2026
Merged

Make PostgreSQL foreign key drops idempotent#27
ksimbili merged 2 commits into
mainfrom
fix/idempotent-pg-foreign-key-drop

Conversation

@ksimbili

@ksimbili ksimbili commented Jul 15, 2026

Copy link
Copy Markdown

TLDR

Makes generated PostgreSQL foreign-key removals idempotent so database publishing does not fail when a cascading table drop already removed the constraint.


Why

Drizzle can generate DROP TABLE ... CASCADE followed by an explicit foreign-key removal. PostgreSQL removes the foreign key during the cascade, so the subsequent DROP CONSTRAINT fails even though the database has reached the intended state.

What changed

PostgreSQL delete_reference statements now render as DROP CONSTRAINT IF EXISTS. This fixes the behavior in the SQL generator rather than rewriting rendered SQL in pid2.

Added schema-diff regression coverage for removing a foreign key.

Validations performed

  • PostgreSQL table schema-diff suite: 30 tests passed
  • Drizzle Kit TypeScript check passed
  • dprint formatting check passed

If this breaks

Generated PostgreSQL migrations may behave differently when removing foreign-key constraints.

Rollout and rollback

Rolls out in the next @drizzle-team/drizzle-kit release. Safe to revert; reverting restores strict failure when a generated foreign-key removal targets an already-absent constraint.

~ written by Zerg 👾 (lurking-cyclone-5507)

Emit DROP CONSTRAINT IF EXISTS for delete_reference statements so a prior cascading table drop cannot make the generated migration fail.
Bump the package version and document the idempotent PostgreSQL foreign-key drop fix.

@joe-thom joe-thom left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

seems fine, we'll need to republish the package and bump in web

@ksimbili
ksimbili merged commit 1c1372f into main Jul 17, 2026
11 of 35 checks passed
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.

2 participants