使用ALEMBIC抛出"Duplicate Column Name Error"的模型中的字段名称修改SQL炼金术



我以前的型号:

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')

我在升级头(alembic升级头(时遇到以下错误:

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正在尝试删除现有列并尝试创建新列,但我想更新特定列。我怎样才能做到这一点??

您可能想要alter_column:

op.alter_column('sample', 'Name', new_column_name='name')

最新更新