Why Flask-migrate cannot upgrade when drop column

Solution 1:

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

Solution 2:

I do not know have you solved the problem. But I find a very concise solution: you can add this to the bottom of your init.py file. Afterwards, delete and rebuild your migrations:

with app.app_context():
    if db.engine.url.drivername == 'sqlite':
        migrate.init_app(app, db, render_as_batch=True)
    else:
        migrate.init_app(app, db)

hope to help you.