使用早于 9.1 的 PostgreSQL 版本(为枚举添加 ALTER TYPE)时,如何在 alembic 迁移中将元素添加到枚举字段? 这个 SO 问题解释了直接过程,但我不太确定如何最好地使用 alembic 来翻译它。
这就是我所拥有的:
new_type = sa.Enum('nonexistent_executable', 'output_limit_exceeded',
'signal', 'success', 'timed_out', name='status')
old_type = sa.Enum('nonexistent_executable', 'signal', 'success', 'timed_out',
name='status')
tcr = sa.sql.table('testcaseresult',
sa.Column('status', new_type, nullable=False))
def upgrade():
op.alter_column('testcaseresult', u'status', type_=new_type,
existing_type=old_type)
def downgrade():
op.execute(tcr.update().where(tcr.c.status==u'output_limit_exceeded')
.values(status='timed_out'))
op.alter_column('testcaseresult', u'status', type_=old_type,
existing_type=new_type)
不幸的是,以上仅在升级时产生 ALTER TABLE testcaseresult ALTER COLUMN status TYPE status
,实际上什么都不做。
原文由 bboe 发布,翻译遵循 CC BY-SA 4.0 许可协议
我决定尝试尽可能直接地遵循 postgres 方法,并提出了以下迁移。
似乎 alembic 在其
alter_table
方法中没有直接支持USING
语句。