1

我以前的模型:

class sample(Base):
    __tablename__ = "sample"
    id = Column(BigInteger, primary_key=True,index=True)
    Name = Column(String(30),index=True)

我的最新型号:

class sample(Base):
    __tablename__ = "sample"
    id = Column(BigInteger, primary_key=True,index=True)
    name = Column(String(30),index=True)

我运行了命令alembic revision --autogenerate -m "changed Name field to name"

我的迁移详细信息生成:

def upgrade():

op.add_column('sample', sa.Column('name', sa.String(length=1000), nullable=False))
op.drop_column('sample', 'Name')

升级 head (alembic upgrade head ) 时出现以下错误:

    sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1060, "Duplicate column name 'name'")
[SQL: ALTER TABLE sampleADD COLUMN `name` VARCHAR(1000) NOT NULL]
(Background on this error at: http://sqlalche.me/e/e3q8)

Alembic 正在尝试删除现有列并尝试创建新列,但我想更新特定列。我怎样才能做到这一点?

4

1 回答 1

0

你可能想要alter_column

op.alter_column('sample', 'Name', new_column_name='name')
于 2020-08-25T23:58:13.330 回答