Conversation
…SCADE (#382) When dropping a table with CASCADE, PostgreSQL automatically removes FK constraints on other tables that reference it. The diff generator was also emitting explicit ALTER TABLE DROP CONSTRAINT for these same FKs, causing "constraint does not exist" errors during apply. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Greptile SummaryThis PR fixes a duplicate-operation error (#382) where dropping a table with Changes:
Findings:
Confidence Score: 5/5Safe to merge; the fix is well-scoped and the one finding is a minor defensive-coding inconsistency that does not affect current behavior. The only finding is a P2 code-quality suggestion (missing empty-schema fallback) that is dormant in practice because the PostgreSQL inspector always populates internal/diff/table.go — the empty-schema fallback inconsistency at line 671 Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[generateModifySQL] -->|passes droppedTables| B[generateModifyTablesSQL]
B --> C{Build droppedTableSet
schema.table → true}
C --> D[For each tableDiff]
D --> E[generateAlterTableStatements
droppedTableSet]
E --> F{For each DroppedConstraint}
F -->|FK constraint?| G{ReferencedTable
in droppedTableSet?}
G -->|Yes - CASCADE handles it| H[skip DROP CONSTRAINT]
G -->|No| I[emit ALTER TABLE DROP CONSTRAINT]
F -->|Not FK| I
Reviews (1): Last reviewed commit: "fix: skip FK constraint drop when refere..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
Fixes a diff/apply failure when dropping a referenced table with CASCADE by avoiding redundant ALTER TABLE ... DROP CONSTRAINT statements for FK constraints that PostgreSQL already removes during the cascading drop.
Changes:
- Pass the set of dropped tables into table-alter SQL generation to suppress redundant FK constraint drops.
- Skip emitting
DROP CONSTRAINTfor FKs whose referenced table is being dropped withCASCADE. - Add a regression fixture for issue #382 under
testdata/diff/create_table/.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
internal/diff/table.go |
Builds a dropped-table lookup and skips FK constraint drops when the referenced table is dropped with CASCADE. |
internal/diff/diff.go |
Threads d.droppedTables into generateModifyTablesSQL so alter generation can filter FK drops. |
testdata/diff/create_table/issue_382_drop_table_cascade_constraint/* |
Adds expected plans/SQL for the cascade-drop scenario to prevent regressions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Mirror the pattern used in shouldDeferConstraint and topological sorting to default empty ReferencedSchema to the owning table's schema. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
When dropping a table with
CASCADE, PostgreSQL automatically removes FK constraints on other tables that reference it. The diff generator was also emitting explicitALTER TABLE ... DROP CONSTRAINTfor these same FKs, causingconstraint "..." does not existerrors during apply.The fix filters out FK constraint drops in
generateAlterTableStatementswhen the referenced table is already being dropped with CASCADE.Fixes #382
Test plan
testdata/diff/create_table/issue_382_drop_table_cascade_constraint/test casePGSCHEMA_TEST_FILTER="create_table/issue_382" go test -v ./internal/diff -run TestDiffFromFilesPGSCHEMA_TEST_FILTER="create_table/" go test -v ./cmd -run TestPlanAndApplycreate_table/anddependency/diff tests pass (no regressions)🤖 Generated with Claude Code