Fix sea-orm-cli generating duplicated columns for multi-column foreign keys#3096
Open
kenkoooo wants to merge 1 commit into
Open
Fix sea-orm-cli generating duplicated columns for multi-column foreign keys#3096kenkoooo wants to merge 1 commit into
kenkoooo wants to merge 1 commit into
Conversation
When a foreign key references a bare unique index (rather than a named unique / primary-key constraint), PostgreSQL's information_schema does not expose the referenced constraint, so schema discovery emits a cartesian product of the foreign-key columns (e.g. local `[f_a, f_b]` paired with referenced `[a, b, a, b]`). The generated `Relation` then carried mismatched `from`/`to` column lists, producing malformed SQL joins that match no rows. De-duplicate both column lists (preserving first-seen order) in `From<&TableForeignKey>`, restoring the intended 1:1 column pairing. A well-formed foreign key never repeats a column on either side, so this is a no-op for valid input and only repairs the cartesian-product degeneracy. Adds regression coverage: a unit test for the de-duplication and end-to-end transformer tests for the rendered `belongs_to` relation (2- and 3-column keys) plus the inverse HasOne/HasMany classification. Fixes SeaQL#2662
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.
sea-orm-cli generate entitygenerated a malformedRelationfor a multi-column foreign key, duplicating columns in thefrom/toattributes and producing SQL joins whoseONclause matches no rows. This de-duplicates the foreign-key column lists when building the relation, restoring the intended 1:1 column pairing.PR Info
New Features
Bug Fixes
Multi-column foreign keys that reference a bare unique index generated duplicated columns.
When a FK targets a bare unique index (
CREATE UNIQUE INDEX …) rather than a namedUNIQUE/PRIMARY KEYconstraint, PostgreSQL'sinformation_schemadoes not expose the referenced constraint (referential_constraints.unique_constraint_nameis empty and the columns are absent fromkey_column_usage). Schema discovery cannot correlate the local and referenced columns by ordinal position and falls back to their cartesian product — local[f_a, f_b]paired with referenced[a, b, a, b].From<&TableForeignKey> for Relationstored those lists verbatim, so the generatedbelongs_topaired twofromcolumns against fourtocolumns:Fixed by de-duplicating both column lists (preserving first-seen order). A well-formed never repeats a column on either side, so it is a no-op for valid input and only repairs the cartesian-product degeneracy. (The root cause is in sea-schema's discovery query; this is a defensive codegen-side fix that a future sea-schema change could complement.)
Breaking Changes
Changes
Verified with cargo test --workspace and cargo test --manifest-path sea-orm-cli/Cargo.toml, and by running sea-orm-cli generate entity against a live PostgreSQL 17 instance with the issue's exact schema.