How do I execute inserts and updates in an Alembic upgrade script?

前端 未结 3 401
滥情空心
滥情空心 2020-11-29 16:05

I need to alter data during an Alembic upgrade.

I currently have a \'players\' table in a first revision:

def upgrade():
    op.create_table(\'player         


        
3条回答
  •  Happy的楠姐
    2020-11-29 16:53

    You can also use direct SQL see (Alembic Operation Reference) as in the following example:

    from alembic import op
    
    # revision identifiers, used by Alembic.
    revision = '1ce7873ac4ced2'
    down_revision = '1cea0ac4ced2'
    branch_labels = None
    depends_on = None
    
    
    def upgrade():
        # ### commands made by andrew ###
        op.execute('UPDATE STOCK SET IN_STOCK = -1 WHERE IN_STOCK IS NULL')
        # ### end Alembic commands ###
    
    
    def downgrade():
        # ### commands auto generated by Alembic - please adjust! ###
        pass
        # ### end Alembic commands ###
    

提交回复
热议问题