[Sqlalchemy-tickets] Issue #4288: FK Reflection in MSSQL incorrectly identifies duplicate source co
Brought to you by:
zzzeek
From: Sean D. <iss...@bi...> - 2018-06-25 16:49:55
|
New issue 4288: FK Reflection in MSSQL incorrectly identifies duplicate source columns for identically-named PKs in different schemas https://bitbucket.org/zzzeek/sqlalchemy/issues/4288/fk-reflection-in-mssql-incorrectly Sean Dunn: For MSSQL: - When reflecting a database (such as using `autoload` for a table) an exception can be thrown `ArgumentError: ForeignKeyConstraint with duplicate source column references are not supported.` Even when there are no duplicate source column references in the database. This is caused by two tables in two schemas with the same Primary Key names -- SqlAlchemy confuses these two Primary Keys as being the referred column for a Foreign Key, when only one of them is. This is because SqlAlchemy isn't also checking the constraint schema. To reproduce: - Create two schemas FOO and BAR - Create three tables, FOO.FOOTABLE, FOO.BAZTABLE, BAR.BARTABLE - Create a PRIMARY KEY on FOO.BAZTABLE and BAR.BARTABLE with the same PK names (i.e. `CONSTRAINT [MY_PK] PRIMARY KEY CLUSTERED ([MY_PK_COL]) - Create a FK in FOO.FOOTABLE that references FOO.BAZTABLE.MY_PK_COL - reflect `FOO.FOOTABLE` via `footable = Table('FOOTABLE', fooschemameta, autoload=True, autoload_with=myengine)` Here's one example of someone else who has had the same problem: https://stackoverflow.com/questions/47205433/sqlalchemy-table-reflection-remove-duplicate-source-column-references/51024467#51024467 The root issue is in `dialects/mssql/base.py` in `def_foreign_keys` -- the query that checks the foreign key constraints does not have a WHERE clause that matches the `R.c.constraint_schema` to `RR.c.unique_constraint_schema`. -- so it's not uniquely identifying the PK (PK's can have identical names in different schemas, so we also need to check the schema) |