fix: defer FK constraints until all table batches are created#229
fix: defer FK constraints until all table batches are created#229
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where foreign key constraints were being applied too early when tables are split into function-dependent and non-function-dependent batches during schema migration. The fix ensures that all foreign key constraints are deferred until after both batches of tables are created, preventing failures when a table without function dependencies has a foreign key to a table with function dependencies (e.g., tables with generated columns).
Key Changes:
- Consolidated deferred FK constraint application to occur after both table batches are created
- Added comprehensive test case for FK constraints referencing tables with generated columns
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/diff/diff.go |
Removed premature FK constraint application after first batch and consolidated both batches' deferred constraints to be applied after all tables are created |
testdata/diff/dependency/table_fk_to_generated_column/new.sql |
Test schema defining a function, a table with a generated column using that function, and a table with FK to the generated column table |
testdata/diff/dependency/table_fk_to_generated_column/old.sql |
Empty starting schema for the test case |
testdata/diff/dependency/table_fk_to_generated_column/diff.sql |
Expected DDL output showing correct ordering: FK-source table, function, FK-target table with generated column, then FK constraint |
testdata/diff/dependency/table_fk_to_generated_column/plan.sql |
Same as diff.sql - expected migration SQL |
testdata/diff/dependency/table_fk_to_generated_column/plan.txt |
Human-readable plan output showing the migration summary and DDL |
testdata/diff/dependency/table_fk_to_generated_column/plan.json |
Structured JSON representation of the migration plan with all steps and metadata |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When tables are split into function-dependent and non-function-dependent batches, FK constraints from the first batch were applied before the second batch was created. This caused failures when a table without function dependencies had a FK to a table with function dependencies (e.g., generated columns). The fix consolidates deferred FK constraint application to run after both table batches are created. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <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.
…#225) (pgplex#229) When tables are split into function-dependent and non-function-dependent batches, FK constraints from the first batch were applied before the second batch was created. This caused failures when a table without function dependencies had a FK to a table with function dependencies (e.g., generated columns). The fix consolidates deferred FK constraint application to run after both table batches are created. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Fix #225
When tables are split into function-dependent and non-function-dependent batches, FK constraints from the first batch were applied before the second batch was created. This caused failures when a table without function dependencies had a FK to a table with function dependencies (e.g., generated columns).
The fix consolidates deferred FK constraint application to run after both table batches are created.
🤖 Generated with Claude Code