Alembic drops and creates foreign key constraints (autogenerate) when the table has different schema than the referenced columns #1064
Unanswered
jrafols-imogate
asked this question in
Usage Questions
Replies: 1 comment 1 reply
-
there's a few ways "dbo" can be happening there and it's all based on a combination of what the foreign key reports as its target schema (which seems to be "dbo" here) and what the "SELECT schema_name()" query reports on your database connection. if those don't match, then you'll see the name set as the "source schema"
so you want to make those match up, or add the explicit schema name to the target foreign key that matches the "dbo" it's coming up with. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to use the autogenerate feature from alembic to update the schema of a MSSQL database.
After executing the command
alembic revision --autogenerate
, this is theupgrade()
function I get:After digging into the code, I found that when alembic compares the foreign keys between the schema and the model in the code, sqlalchemy forces a
dbo
schema to the Table1.ID referenced column:Database foreign key:
('foo', 'Table2', ('Table1ID',), 'dbo', 'Table1', ('ID',), None, None, 'not deferrable')
Model foreign key:
('foo', 'Table2', ('Table1ID',), None, 'Table1', ('ID',), None, None, 'not deferrable')
This difference leads to the drop and create commands in the
upgrade()
function later on. If I remove the{'schema': 'foo'}
in__table_args__
the issue disappears so my guess is that the table schema (different from the default one) forces the schema on the foreign key reference.I would like to know if there is any way to overcome this problem.
Beta Was this translation helpful? Give feedback.
All reactions