Making Flask Migrations
Learn how to make migrations in Flask.
We'll cover the following...
Creating a migration file
We must create a migration file to make migrations in our Core app. Let’s create a migration file in the backendservice2 folder named manager.py. Then, we can open the file and import the following modules:
from core import corefrom core import dbfrom flask_migrate import Migratefrom flask_migrate import MigrateCommandfrom flask_script import Manager
- Lines 1–2: We import our Flask application instance, - core, and our MySQL database instance,- db, from our- core.py.
- Lines 3–4: We import - Migrateand the- MigrateCommandfrom- flask_migrate.
- Line 5: We import the - Managerfrom- flask_script.
Now that we are done with the imports necessary for our migration file to function correctly, we can handle migrations in the following way:
migrate = Migrate(core, db)manager = Manager(core)manager.add_command('db', MigrateCommand)if __name__ == '__main__':manager.run()
- Line 1: We create an instance of - Migrate,- migrate, passing our Flask application instance- coreand database- dbas arguments.
- Line 3: ...