基本上,我需要做的是重命名列(保存数据),删除旧的外键(约束?)然后创造一个新的。在
以下是我的迁移脚本:from alembic import op
import sqlalchemy as sa
def upgrade():
op.rename_table('organs', 'purposes')
op.alter_column('orders', 'organ_id', new_column_name='purpose_id', existing_type=sa.Integer)
op.drop_constraint(constraint_name="orders_ibfk_2", table_name="orders", type_="foreignkey")
op.create_foreign_key(
constraint_name="orders_ibfk_2",
source_table="orders",
referent_table="purposes",
local_cols=["purpose_id"],
remote_cols=["id"])
def downgrade():
op.rename_table('purposes', 'organs')
op.alter_column('orders', 'purpose_id', new_column_name='organ_id', existing_type=sa.Integer)
op.drop_constraint(constraint_name="orders_ibfk_2", table_name="orders", type_="foreignkey")
op.create_foreign_key(
constraint_name="orders_ibfk_2",
source_table="orders",
referent_table="organs",
local_cols=["organ_id"],
remote_cols=["id"])