Why Flask-migrate cannot upgrade when drop column

前端 未结 3 1687
别那么骄傲
别那么骄傲 2020-12-23 10:03

I am using SqlAlchemy and Flask-migrate for DB migration. I have successfully init the DB and upgrade once, but when I deleted one of my table colu

3条回答
  •  时光取名叫无心
    2020-12-23 10:21

    SQLite does not support dropping or altering columns. However, there is a way to work around this by making changes at the table level: https://www.sqlite.org/lang_altertable.html

    And more usefully for Alembic/Flask-Migrate users, Alembic's batch_alter_table context manager lets you specify the changes in a natural way, and does a little "make new table - copy data - drop old table - rename new table" dance behind the scenes when using SQLite. See: http://alembic.zzzcomputing.com/en/latest/batch.html

    So the upgrade() function in your migration file should contain something like:

    with op.batch_alter_table('posts') as batch_op:
        batch_op.drop_column('tags')
    

    I'm afraid I don't know why the error changed the second time you tried the upgrade.

    As tkisme points out, you can also configure the EnvironmentContext.configure.render_as_batch flag in env.py so that autogenerated migration scripts will use batch_alter_table by default. See: http://alembic.zzzcomputing.com/en/latest/batch.html#batch-mode-with-autogenerate

提交回复
热议问题