Adding Flask-Migrate to an Existing Flask-SQLAlchemy Project

Posted by


Flask-Migrate is a Flask extension that helps you manage database migrations in a Flask-SQLAlchemy project. This tutorial will guide you through the process of adding Flask-Migrate to an existing Flask-SQLAlchemy project.

Step 1: Install Flask-Migrate
The first step is to install Flask-Migrate. You can do this using pip:

pip install Flask-Migrate

Step 2: Create a Migration Repository
After installing Flask-Migrate, you need to create a migration repository. To do this, run the following command in your project directory:

flask db init

This command will create a ‘migrations’ directory in your project with a ‘versions’ subdirectory where all the migration scripts will be stored.

Step 3: Configure Flask-Migrate
Next, you need to configure Flask-Migrate in your Flask application. Add the following lines of code to your Flask application:

from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)

In this code snippet, we are initializing Flask-Migrate with our Flask application and SQLAlchemy database.

Step 4: Create an Initial Migration
Now that Flask-Migrate is configured in your Flask application, you need to create an initial migration that represents the current state of your database. To do this, run the following command:

flask db migrate -m "initial migration"

This command will generate a migration script in the ‘versions’ directory with the changes required to create your database tables.

Step 5: Apply the Migration
After creating the initial migration, you need to apply it to your database. To do this, run the following command:

flask db upgrade

This command will execute the migration script and apply the changes to your database.

Step 6: Create and Apply Subsequent Migrations
Whenever you make changes to your database schema, you need to create a new migration and apply it to your database. To create a new migration, run the following command:

flask db migrate -m "description of changes"

This will generate a new migration script in the ‘versions’ directory with the changes you made to your database schema. To apply the new migration, run the following command:

flask db upgrade

This will execute the new migration script and apply the changes to your database.

By following these steps, you can easily add Flask-Migrate to an existing Flask-SQLAlchemy project and manage database migrations effectively. Happy coding!

0 0 votes
Article Rating

Leave a Reply

14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@jeffersonmiguel6790
12 days ago

I have an error when trying to perform db migrate, a utf-8 encoding error is occurring, I am unable to resolve it.

@atillaalagoz4100
12 days ago

Thank you 🍀

@lator1941
12 days ago

1. flask db migrate (this generates migration script for development DB)
2. flask db upgrade (this runs the migration script)
After connecting to production DB
3. flask db stamp head (this literally stamps the head revision migration into the production DB – doesn't change schema)
4. flask db upgrade (runs the migration script and upgrades the production DB to the head revision)

I had issues with this. Here's what happened. I had existing migration files for my project, and after I ran `flask db migrate` and `flask db upgrade` for my development DB, it worked as expected. But on switching the DB to the production one, after `flask db stamp head` and `flask db upgrade` the production database did not reflect the new schema changes yet `flask upgrade` ran w/o errors.
When I ran `flask db check`, it showed the new schema changes detected but they were unapplied. It was quite baffling.
I was able to resolve this by

1. stamping production db with last version before head e.g
flask db stamp <last-revision-before-head>

2. run flask db upgrade with revision id of head:
flask db upgrade <revision-id-of-head/latest-migration-script>

@lator1941
12 days ago

The Flask community is indebted to you, Anthony Hubert. Thank you for all your helpful videos.

@vigneshkumar2778
12 days ago

Is there any way to run init db and migrate db in the code itself instead of cli

@AliMughrabi
12 days ago

Thanks, how to open PostgreSQL in browser ?

@andre9755
12 days ago

I am struggling, because i am on a windows machine and my terminal doesn't do the same thing as in the video

i am using VSCode and my terminal looks like this
"PS C:…>"

can anyone tell me what i need to do to get to the terminal with the "$"

thanks ❤

@tjkarthik2
12 days ago

You are pretty smart to skip the major real time problem… Scenario is, If there are some tables with relationships in your db already, when you run the migarate it will generate migration file for creating the tables and dropping, so you cant run either upgrade or downgrade on that time. because there will not be any cascading delete. so it will throw errors like, these tables have some dependencies

@cascossi809
12 days ago

Thank you very much Anthony. You explain concepts very well and the project example you deliver is easy to hand on.

@chrisbanas9118
12 days ago

Extremely helpful, the blank DB trick fixed my problems

@miiihaaas
12 days ago

2:30 I have error: KeyError: 'migrate'
Do you have any hint how to solve this problem? I've spent some time googling for solution but I failed

@stanjustine3179
12 days ago

why docker file
?

@paulcurious2324
12 days ago

great one again! thanks for this

@dmbrv
12 days ago

Nice video. Very useful. Thanks

14
0
Would love your thoughts, please comment.x
()
x