- Flask Framework Cookbook(Second Edition)
- Shalabh Aggarwal
- 157字
- 2021-06-24 13:58:06
Migrating databases using Alembic and Flask-Migrate
Now, let's say we want to update our models to have a new field called company in the Product model. One way is to drop the database and then create a new one using db.drop_all() and db.create_all(). However, this approach cannot be followed for applications in production or even in staging. We would want to migrate our database to match the newly updated model with all the data intact.
For this, we have Alembic, which is a Python-based tool to manage database migrations and uses SQLAlchemy as the underlying engine. Alembic provides automatic migrations to a great extent with some limitations (of course, we cannot expect any tool to be seamless). To act as the icing on the cake, we have a Flask extension called Flask-Migrate, which eases the process of migrations even more. In this recipe, we will cover the basics of database migration techniques using Alembic and Flask-Migrate.