数据迁移bug修复

This commit is contained in:
andy committed 2026-08-25 11:46:52 +08:00
1 parent 45c879328b
commit 202444353e
2 files changed
+82

No files matched your search

@@ -40,6 +40,20 @@ def _drop_column_with_foreign_keys(table_name: str, column_name: str) -> None:
op.drop_column(table_name, column_name)
def _drop_referencing_foreign_keys(table_name: str) -> None:
bind = op.get_bind()
inspector = inspect(bind)
for current_table in inspector.get_table_names():
if current_table == table_name:
continue
for foreign_key in inspector.get_foreign_keys(current_table):
if foreign_key.get("referred_table") != table_name:
continue
constraint_name = foreign_key.get("name")
if constraint_name:
op.drop_constraint(constraint_name, current_table, type_="foreignkey")
def upgrade() -> None:
bind = op.get_bind()
existing_tables = set(inspect(bind).get_table_names())
@@ -49,6 +63,7 @@ def upgrade() -> None:
for table_name in (*PRODUCT_TABLES[:3], "RouteSection", "DemandRecommendation", *PRODUCT_TABLES[3:]):
if table_name in existing_tables:
_drop_referencing_foreign_keys(table_name)
op.drop_table(table_name)