admin管理员组文章数量:1202780
After the autogenrated initial migration, when running another autogeneration all foreign keys are beeing dropped and recreated.
Example Code from the migration script:
op.drop_constraint('fk_foo_bar_id_bar', 'foo', type_='foreignkey')
op.create_foreign_key(op.f('fk_foo_bar_id_bar'), 'foo', 'bar', ['bar_id'], ['id'], source_schema='public', referent_schema='public')
I tried changing env.py include_object as well as the way migrations were run. I also tried changing both foo and bar model in any way that seemed reasonable. Naming the FKs also didnt work.
After the autogenrated initial migration, when running another autogeneration all foreign keys are beeing dropped and recreated.
Example Code from the migration script:
op.drop_constraint('fk_foo_bar_id_bar', 'foo', type_='foreignkey')
op.create_foreign_key(op.f('fk_foo_bar_id_bar'), 'foo', 'bar', ['bar_id'], ['id'], source_schema='public', referent_schema='public')
I tried changing env.py include_object as well as the way migrations were run. I also tried changing both foo and bar model in any way that seemed reasonable. Naming the FKs also didnt work.
Share Improve this question asked Jan 21 at 3:49 tnfrutnfru 3541 silver badge11 bronze badges 1- Please edit the question to include a sample model that generates this behaviour, and the construction of the metadata object (for example, naming conventions). – snakecharmerb Commented Jan 21 at 10:48
1 Answer
Reset to default 0The solution lies in removing the explicit definition of the public schema in your classes.
So if you have this in your bar class
bar_id: Mapped[Integer] = mapped_column(
ForeignKey("public.bar.id"), index=True, type_=Integer
)
And in your foo class
__table_args__ = (
{"schema": "public"},
)
Remove both public statements. Postgres does sometimes not explicitly name it that way and alembic then thinks it is a different FK and tries to recreate them.
本文标签: pythonAlembic keeps deleting and recreating Foreign Keys during autogenrationStack Overflow
版权声明:本文标题:python - Alembic keeps deleting and recreating Foreign Keys during autogenration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738654792a2105079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论