How to Perform Flask-SQLAlchemy Migrations Using Flask-Migrate

To apply the migrations to a database, these migration scripts are executed in the sequence they were created. The trick to generate this initial migration for a project that uses an up to date database is to temporarily switch to an empty database, just for the flask db migrate command. In my projects I use a DATABASE_URL environment variable to configure the location of the database.

flask migration

The sqlalchemy module includes general purpose database functions and classes such as types and query building helpers, while sqlalchemy.orm provides the support for using models. Given that these two module names are long and will need to be referenced often, the sa and so aliases are defined directly in the import statements. flask developer The db instance from Flask-SQLAlchemy and the Optional typing hint from Python are imported as well. The nice thing about SQLAlchemy is that it is an ORM not for one, but for many relational databases. SQLAlchemy supports a long list of database engines, including the popular MySQL, PostgreSQL and SQLite.

Step 04 – Modifying the Database Model

This is just a demo to give you an idea how to deploy when using a migration library like Flask-Migrate/Alembic. Alembic is good at simple migrations like creating tables, adding/renaming columns etc. I hope this short tutorial clarifies how to add database migrations to your existing project. If you want to avoid the extra trouble required to get that first migration generated, in your next project consider adding Flask-Migrate from the start.

  • You are welcome to inspect the script if you are curious to see how it looks.
  • This happens because SQLAlchemy configures integer primary key columns to be auto-incrementing.
  • After you run the flask db init command, you will find a new migrations directory, with a few files and a versions subdirectory inside.
  • In terms of the actual database migrations, everything is handled by Alembic so you get exactly the same functionality.
  • The where() clause is used to create filters that select only a subset of the rows from the entity selected.

This is going to be the topic of another chapter, so don’t worry about it too much for now. The id field is usually in all models, and is used as the primary key. Each user in the database will be assigned a unique id value, stored in this field. Primary keys are, in most cases, automatically assigned by the database, so I just need to provide the id field marked as a primary key. Before creating the docker container, let’s modify app.py to be a little bit more useful, by automatically creating a post on app startup in order for us to have some actual data in the database. In addition, let’s modify the existing web route to fetch posts from the database.

How to Perform Flask-SQLAlchemy Migrations Using Flask-Migrate

This user_id field is called a foreign key, because it references a primary key of another table. The database diagram above shows foreign keys as a link between the field and the id field of the table it refers to. This kind of relationship is called a one-to-many, because “one” user writes “many” posts. The output of the command gives you an idea of what Alembic included in the migration. The first two lines are informational and can usually be ignored.

flask migration

Instead of running the latest migration, you can also pass the revision id of the migration you want to run. In this case, db upgrade would stop after running the specified migration and will not proceed to run the latest migration. We created templates using Jinja2 and HTML to display the users in a table and allow users to add new users. We used request and redirect functions to handle form submissions and navigate between pages.

Bad gateway error for a Flask app deployed using Elastic Beanstalk

The tables were created successfully in the new database and we were able to interact with the tables with the newly defined endpoints. Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database https://remotemode.net/ Toolkit for Python. In order to use Flask-Migrate we imported Manager as well as Migrate and MigrateCommand to our manage.py file. We also imported app and db so we have access to them from within the script.

Leave a Comment

Your email address will not be published. Required fields are marked *