Repository navigation
HCK-18372: pair composite foreign key columns in the right order - #149
Merged
Merged
Conversation
…e constraint Co-authored-by: Cursor <cursoragent@cursor.com>
|
taras-dubyk
reviewed
Oct 30, 2026
| ON PK.table_schema = REF.table_schema | ||
| AND PK.table_name = REF.table_name | ||
| AND PK.position_in_unique_constraint IS NULL | ||
| AND PK.ordinal_position = FK.position_in_unique_constraint |
Contributor
There was a problem hiding this comment.
I suppose we may want to add sorting by constraint name if the flakiness continues, to ensure we always get the same order.
ORDER BY FK.constraint_name.
taras-dubyk
approved these changes
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



## Content
Reverse-engineering a BigQuery table with a composite foreign key sometimes linked each child column to the wrong parent column (for example
a_fk -> int_idandb_fk -> idinstead ofa_fk -> idandb_fk -> int_id). The result changed from one run to the next.This is also why the Studio feature test
test/feature/reverseEngineering/databases/bigquery.spec.js("Reverse-Engineer from instance") fails from time to time on thecomposite_fkrelationship.Technical details
CONSTRAINT_COLUMN_USAGE(parent columns) withKEY_COLUMN_USAGE(child columns) only onconstraint_name. For a FK on 2 columns it returned the 4 combinations, in a random order, andreverseForeignKeysbuilt the child and parent lists separately from that order.KEY_COLUMN_USAGE.position_in_unique_constraint, and returnsordinal_position. One row = one (child, parent) pair.reverseForeignKeysbuilds one relationship per constraint, with columns sorted byordinal_position. The order of the relationships themselves is unchanged (first appearance), so the Studio expected model does not need an update.Checked against the test dataset
hackolade-bigquery-test.MyDataset:composite_fk [b_fk, a_fk] -> [id, int_id]in some runs (same mismatch as on CI);composite_fk [a_fk, b_fk] -> [id, int_id],simpleFKandfk$1unchanged and in the same order asexpectedRE.json.Not checked: the full Studio feature test; the "Plugins, RE from instances" job on this PR should confirm it. FKs referencing a table in another dataset are still not found, same as before.