You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Migrated issue, originally created by Sean Dunn (@seanfdnn)
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)
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)
The text was updated successfully, but these errors were encountered:
Migrated issue, originally created by Sean Dunn (@seanfdnn)
For MSSQL:
autoload
for a table) an exception can be thrownArgumentError: 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:
FOO.FOOTABLE
viafootable = 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
indef_foreign_keys
-- the query that checks the foreign key constraints does not have a WHERE clause that matches theR.c.constraint_schema
toRR.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)The text was updated successfully, but these errors were encountered: