Skip to content

Conversation

@curthasselschwert
Copy link
Contributor

@curthasselschwert curthasselschwert commented Jan 28, 2026

When dumping composite foreign key constraints where the FK column order differs from the table's column definition order, pgschema was outputting referenced columns in the wrong order, causing type mismatch errors when applying the generated DDL.

The bug was in buildConstraints: ForeignOrdinalPosition (fa.attnum - the column's position in the foreign table was used for the referenced column's Position instead of using the local column's constraint position. Since local and referenced columns are paired in the FK definition, they should share the same position value.

Fixes the issue by using the local column's constraint position for the paired referenced column.

Closes #266

…table column order (pgplex#266)

When dumping composite foreign key constraints where the FK column order
differs from the table's column definition order, pgschema was outputting
referenced columns in the wrong order, causing type mismatch errors when
applying the generated DDL.

The bug was in buildConstraints: ForeignOrdinalPosition (fa.attnum - the
column's position in the foreign table was used for the referenced column's
Position instead of using the local column's constraint position. Since local
and referenced columns are paired in the FK definition, they should share
the same position value.

Fixes the issue by using the local column's constraint position for the
paired referenced column.
EOF
)
Copilot AI review requested due to automatic review settings January 28, 2026 20:39
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 in the handling of composite foreign key constraints where the FK column order differs from the table's column definition order. Previously, pgschema incorrectly used the foreign table's column positions (attnum) for referenced columns, causing type mismatches when the FK column order differed from the table definition order.

Changes:

  • Fixed buildConstraints in ir/inspector.go to use the local column's constraint position for paired referenced columns instead of the foreign table's column position
  • Added comprehensive test case composite_fk_column_order demonstrating the bug and verifying the fix

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
ir/inspector.go Fixed position assignment for referenced columns in composite FK constraints to use local constraint position instead of foreign table column position
testdata/diff/create_table/composite_fk_column_order/old.sql Test input: tables without FK constraint
testdata/diff/create_table/composite_fk_column_order/new.sql Test input: tables with composite FK where column order differs from table definition
testdata/diff/create_table/composite_fk_column_order/diff.sql Expected diff output: correct FK constraint DDL
testdata/diff/create_table/composite_fk_column_order/plan.sql Expected plan SQL output
testdata/diff/create_table/composite_fk_column_order/plan.txt Expected plan text output
testdata/diff/create_table/composite_fk_column_order/plan.json Expected plan JSON output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the contribution

@tianzhou tianzhou merged commit 3ce21e5 into pgplex:main Jan 30, 2026
6 of 7 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.

Composite FK referenced column order incorrect when FK order differs from table column order

2 participants