Flask-Migrate tips
Posted on
by Kevin FoongFlask-Migrate is a great tool which allows Flask developers, using SQL Alchemy, to amend their database on the fly. This is important because it means you wont have to destroy your current table and any data within it, in order to make any changes.
Miguel Gringberg (who is also the author of Flask-Migrate and Flask guru) has a great blog post on how to use Flask-Migrate so I won't go through the basics here. I will however provide a few tips that I have learnt along the way.
Out of the box Flask-Migrate does not detect changes you make to the database field's size or type. For example it will not detect if you change a field from varchar (50) to varchar (100).
To solve this, when you instantiate Flask-Migrate, just make sure you add "compare_type = true" like so.
migrate = Migrate(compare_type=True)
Sometimes for whatever reason your migrations can get out of sync and the upgrade will not work. If you get a "Target database is not up to date" error message you can force it to be fully updated.
flask db stamp head
After this you should be able to continue with "flask db migrate" and "flask db upgrade" to update your database.
The other alternative is to start again by deleting your "migrations" folder. You will also need to remove any migration record in the "alembic_version" table in your database. Below is the SQL command for MySQL to delete the table.
DELETE FROM alembic_version;
Once deleted you can then recreate your migration repository and upgrade your database as per normal.
flask db init
flask db migrate -m "first"
flask db upgrade
Tags: flask flask-migrate