我正在尝试使用 Alembic 将 PostgreSQL 中的列类型从 ENUM 更改为 INTEGER(这也是外键)。但是,当我尝试升级迁移时,遇到以下错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.CannotCoerce) 无法将类型 serie_enum 转换为整数 LINE 1: ...R COLUMN serie_aluno TYPE INTEGER USING serie_aluno::integer
[SQL: ALTER TABLE aluno ALTER COLUMN serie_aluno TYPE INTEGER USING serie_aluno::integer]
以下是 Alembic 生成的迁移文件:
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('aluno', 'serie_aluno',
existing_type=postgresql.ENUM('6', '7', '8', '9', name='serie_enum'),
type_=sa.Integer(),
existing_nullable=False,
postgresql_using="NULL")
op.create_foreign_key(None, 'aluno', 'serie', ['serie_aluno'], ['id_serie'])
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'aluno', type_='foreignkey')
op.alter_column('aluno', 'serie_aluno',
existing_type=sa.Integer(),
type_=postgresql.ENUM('6', '7', '8', '9', name='serie_enum'),
existing_nullable=False)
# ### end Alembic commands ###
如何才能成功地将列从 ENUM 转换为 INTEGER,同时确保正确创建外键约束?