改变Alembic的主键?

时间:2015-03-12 09:50:03

标签: python sqlalchemy alembic

我已阅读the docs,但我无法在任何地方找到相关说明。我尝试删除旧密钥并添加一个新密钥,但这会让我产生错误:

op.drop_constraint('PRIMARY', 'some_table', type_='primary')
op.create_primary_key('PRIMARY', 'some_table', ['col1', 'col2'])

sqlalchemy.exc.OperationalError: (OperationalError) (1025, "Error on rename of ... (errno: 150 - Foreign key constraint is incorrectly formed)") 'ALTER TABLE some_table DROP PRIMARY KEY ' ()

我做错了什么?

2 个答案:

答案 0 :(得分:10)

我也遇到了同样的情况:改变主键。在我的例子中,我不得不将主键类型从整数更改为字符串。

主键还与另一个表具有外键关系。早期的alembic迁移以下列方式创建了外键约束:

#!/usr/bin/python3

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.create_table('user',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('username', sa.String(length=100), nullable=False),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_user')),
                    sa.UniqueConstraint('username', name=op.f('uq_user_username'))
                    )

    op.create_table('role',
                    sa.Column('id', sa.Integer, primary_key=True),
                    sa.Column('name', sa.String(100)),
                    sa.Column('description', sa.String(255)),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_role'))
                    )

    op.create_table('roles_users',
                    sa.Column('user_id', sa.Integer, nullable=True),
                    sa.Column('role_id', sa.Integer, nullable=True),
                    sa.ForeignKeyConstraint(['user_id'], ['user.id'],
                                            name=op.f('fk_roles_user_user_id_user')),
                    sa.ForeignKeyConstraint(['role_id'], ['role.id'],
                                            name=op.f('fk_roles_user_role_id_role'))
                    )

现在,在将user表的主键类型从Integer更改为String时,我必须执行以下操作:

from alembic import op
import sqlalchemy as sa


def upgrade():
    # Drop primary key constraint. Note the CASCASE clause - this deletes the foreign key constraint.
    op.execute('ALTER TABLE user DROP CONSTRAINT pk_user CASCADE')
    # Change primary key type
    op.alter_column('user', 'id', existing_type=sa.Integer, type_=sa.VARCHAR(length=25))
    op.alter_column('roles_users', 'user_id', existing_type=sa.Integer, type_=sa.VARCHAR(length=25))
    # Re-create the primary key constraint
    op.create_primary_key('pk_user', 'user', ['id'])
    # Re-create the foreign key constraint
    op.create_foreign_key('fk_roles_user_user_id_user', 'roles_users', 'user', ['user_id'], ['id'], ondelete='CASCADE')

Flask版本:0.12.1

Alembic版本:0.9.1

Python版本:3.4.4

希望这些信息可以帮助遇到类似问题的人。

答案 1 :(得分:5)

我遇到了这个问题,寻找示例迁移。所以这是我的完全迁移,它删除了PK约束并添加了一个新的AUTO INCREMENT PK:

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


def upgrade():
    op.drop_constraint('PRIMARY', 'similar_orders', type_='primary')

    op.execute("ALTER TABLE similar_orders ADD COLUMN id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT")


def downgrade():
    op.drop_column('similar_orders', 'id')

    op.create_primary_key("similar_orders_pk", "similar_orders", ["order_id", ])

在列上改变PK在alembic中不起作用,请改用drop_constraint,参见here。希望这有帮助!