Skip to content

Comments

fix: defer FK constraints until all table batches are created#229

Merged
tianzhou merged 1 commit intomainfrom
dependency
Jan 5, 2026
Merged

fix: defer FK constraints until all table batches are created#229
tianzhou merged 1 commit intomainfrom
dependency

Conversation

@tianzhou
Copy link
Contributor

@tianzhou tianzhou commented Jan 5, 2026

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

Copilot AI review requested due to automatic review settings January 5, 2026 05:59
@tianzhou tianzhou changed the title fix: defer FK constraints until all table batches are created (#225) fix: defer FK constraints until all table batches are created Jan 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@tianzhou tianzhou merged commit 59ce032 into main Jan 5, 2026
8 checks passed
@tianzhou tianzhou deleted the dependency branch January 5, 2026 14:12
alecthomas pushed a commit to alecthomas/pgschema that referenced this pull request Jan 26, 2026
…#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>
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] dump: table created after reference when generating a column with a function

1 participant