-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect Discovery for Composite Foreign Keys in PostgreSQL #100
Comments
Ping for @julmaxi |
Thanks for testing. I will create a pull request. |
@julmaxi May I send this as pull request? |
* Fixes SeaQL#100 * Should also fix SeaQL/sea-orm#1690
* Fix incorrect discovery of composite foreign keys in PostgreSQL * Fixes #100 * Should also fix SeaQL/sea-orm#1690 * fmt * Add test cases --------- Co-authored-by: Billy Chan <[email protected]>
I've run into a semi-related issue where I still couldn't get the code to generate correctly in a composite foreign key but the problem was that I had a foreign key that referenced something that has a unique index but not a unique constraint, also see this: https://stackoverflow.com/questions/61249732/null-values-for-referential-constraints-unique-constraint-columns-in-informati The fix was to run code like this: ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE USING INDEX index_name; And then the constraint is correctly referenced in the information_schema |
Description
Postgres schema discovery produces incorrect results for composite foreign keys.$n$ columns each key is repeated $n$ times in both
Specifically, for a key with
columns
andforeign_columns
.Steps to Reproduce
On a Postgres database:
Expected Behavior
The references section for the example should like this:
Actual Behavior
Reproduces How Often
Always
Versions
sea-schema=0.11.0
Postgres 14.6
Additional Information
I am relatively confident that the root cause is this query:
sea-schema/src/postgres/query/constraints/mod.rs
Lines 134 to 160 in e709644
Specifically, the inner select will produce all primary keys targeted by a foreign key. Each of these will be joined with all referencing foreign key columns in a matching child table. This produces all$n \times n$ possible combinations of primary and foreign key columns which is of course fine when $n=1$ but not in any other case.
One potential way to fix this is to additionally query the
ordinal_position
of each column in the primary key in the inner select.This requires joining with the
key_column_usage
table in the inner select to retrieve the primary key.ordinal_position
can then be matched withposition_in_unique_constraint
in thekey_column_usage
row for the foreign key.The overall query would look like this (changes commented)
However, I am not sure if I overlook any side effects of this fix.
The text was updated successfully, but these errors were encountered: