Alembic Drops and Recreates Existing Foreign Key with Same Name When Adding Another Column #1663
Replies: 1 comment 10 replies
-
hi - it looks like the means by which Alembic is reflecting a foreign key from your database does not include "schema" information while the foreign key you indicate in your metadata includes a foreign key with a schema of "test_2". So these are two totally different FKs from Alembic's perspective. Also this is not indicated but it looks from your object names that the driver in use is the DB2 driver? I dont know the state of the DB2 driver's reflection capabilities but correct round-tripping of schema information tends to be a more advanced use case. if your DB2 connection works such that "test_2" is the default schema then you want to omit this schema name from your models. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When modifying the
Phone
model:class Phone(...): user_id: Mapped[int] = mapped_column(ForeignKey(User.id)) + description: Mapped[str] = mapped_column(String(50), nullable=False)
Alembic generates a migration that deletes and recreates the existing
user_id
foreign key even though the constraint name appears unchanged. The migration output shows:Resulting in this upgrade script:
Key Observations:
fk_phones_user_id_users
) remains identical.source_schema
andreferent_schema
.Question:
Why does Alembic treat this as a constraint change requiring deletion/recreation instead of recognizing it as the same constraint? Is this related to schema-awareness in constraint comparisons, even when the constraint name remains unchanged?
Beta Was this translation helpful? Give feedback.
All reactions