如何在烧瓶迁移中重命名表

时间:2021-06-23 13:37:25

标签: python sqlalchemy alembic

我有一个名为 company 的模型,我想将其重命名为 accounts 并且其中有一些数据,但是每当我在制作后运行 flask migrate 命令时模型名称的变化,它实际上创建了这个迁移文件:

"""empty message

Revision ID: ead37a7b46e2
Revises: 1463685ab18f
Create Date: 2021-06-23 18:58:51.575071

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'ead37a7b46e2'
down_revision = '1463685ab18f'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('accounts',
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('slug', sa.String(length=3), nullable=True),
    sa.Column('website_url', sa.String(length=2000), nullable=True),
    sa.Column('phone', sa.String(length=30), nullable=True),
    sa.Column('company_upload_directory', sa.String(length=50), nullable=True),
    sa.Column('max_users', sa.Integer(), nullable=True),
    sa.Column('can_supply', sa.Integer(), nullable=False),
    sa.Column('can_demand', sa.Integer(), nullable=True),
    sa.Column('email', sa.String(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.drop_table('company')
    op.drop_constraint('address_ibfk_1', 'address', type_='foreignkey')
    op.create_foreign_key(None, 'address', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('company_groups_ibfk_1', 'company_groups', type_='foreignkey')
    op.create_foreign_key(None, 'company_groups', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('company_plan_ibfk_1', 'company_plan', type_='foreignkey')
    op.create_foreign_key(None, 'company_plan', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('company_services_ibfk_1', 'company_services', type_='foreignkey')
    op.create_foreign_key(None, 'company_services', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('company_users_ibfk_1', 'company_users', type_='foreignkey')
    op.create_foreign_key(None, 'company_users', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('service_company_ibfk_1', 'service_company', type_='foreignkey')
    op.create_foreign_key(None, 'service_company', 'accounts', ['company_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'service_company', type_='foreignkey')
    op.create_foreign_key('service_company_ibfk_1', 'service_company', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'company_users', type_='foreignkey')
    op.create_foreign_key('company_users_ibfk_1', 'company_users', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'company_services', type_='foreignkey')
    op.create_foreign_key('company_services_ibfk_1', 'company_services', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'company_plan', type_='foreignkey')
    op.create_foreign_key('company_plan_ibfk_1', 'company_plan', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'company_groups', type_='foreignkey')
    op.create_foreign_key('company_groups_ibfk_1', 'company_groups', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'address', type_='foreignkey')
    op.create_foreign_key('address_ibfk_1', 'address', 'company', ['company_id'], ['id'], ondelete='CASCADE')
    op.create_table('company',
    sa.Column('created_at', mysql.DATETIME(), nullable=True),
    sa.Column('updated_at', mysql.DATETIME(), nullable=True),
    sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
    sa.Column('name', mysql.VARCHAR(length=255), nullable=False),
    sa.Column('slug', mysql.VARCHAR(length=3), nullable=True),
    sa.Column('website_url', mysql.VARCHAR(length=2000), nullable=True),
    sa.Column('phone', mysql.VARCHAR(length=30), nullable=True),
    sa.Column('company_upload_directory', mysql.VARCHAR(length=50), nullable=True),
    sa.Column('max_users', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
    sa.Column('can_supply', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
    sa.Column('can_demand', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
    sa.Column('email', mysql.VARCHAR(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_default_charset='latin1',
    mysql_engine='InnoDB'
    )
    op.drop_table('accounts')
    # ### end Alembic commands ###

正如您在迁移文件中看到的,它使用了 op.drop_table('company'),因此我收到外键约束错误。我认为,而不是这些,它应该使用类似 rename table 命令的东西。

任何人都可以在这里帮助我吗,我如何使用烧瓶迁移重命名表?

0 个答案:

没有答案