Created
June 20, 2020 05:58
-
-
Save ricky-lim/dca855169e315b5faa9e67efe80cc942 to your computer and use it in GitHub Desktop.
Flask DB migrate to drop column with sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example to drop a column title in a posts table: | |
# Generate by Alembic | |
def downgrade(): | |
# ### commands auto generated by Alembic - please adjust! ### | |
op.drop_index(op.f('ix_posts_title'), table_name='posts') | |
op.drop_column('posts', 'title') | |
$ flask db downgrade | |
.... | |
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "DROP": syntax error | |
# Modify the downgrade by `batch_op`: | |
# creating a new table with a column removed, | |
# dropping the old table | |
# renaming the new table with the old table's name | |
def downgrade(): | |
with op.batch_alter_table("posts") as batch_op: | |
# batch_op.drop_index(op.f('ix_posts_title')) | |
# Remove the table name | |
batch_op.drop_column('title') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment