Skip to main content
 首页 » 编程设计

python之Alembic 和枚举类型

2024年11月01日11小虾米

为什么下面的代码不起作用?

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') 
op.alter_column('testcaseresult', u'status', type_=new_type, 
                existing_type=old_type) 

请您参考如下方法:

Alembic 在枚举字段方面存在问题

尝试使用这个例子

from sqlalchemy.dialects import postgresql     
 
def upgrade(): 
    priority = postgresql.ENUM('H', 'M', 'L', name='priority') 
    priority.create(op.get_bind()) 
    # ### commands auto generated by Alembic - please adjust! ### 
    op.add_column('tasks', sa.Column('priority', sa.Enum('H', 'M', 'L', name='priority'), nullable=True)) 
    # ### end Alembic commands ### 
 
 
def downgrade(): 
    # ### commands auto generated by Alembic - please adjust! ### 
    op.drop_column('tasks', 'priority') 
    priority = postgresql.ENUM('H', 'M', 'L', name='priority') 
    priority.drop(op.get_bind()) 
    # ### end Alembic commands ###