06 - Making Migrations
In this module we will look at migrations, what they are, and connecting to our database for the first time
Last updated
Was this helpful?
In this module we will look at migrations, what they are, and connecting to our database for the first time
Last updated
Was this helpful?
Django's manage.py
file is amazing. It comes with many different commands and flags and options that do anything you could possibly need while making a full stack web app. Flask is not so great, and doesn't come with a manage.py
file. But, that doesn't mean we can't make one ourselves. Our manage.py
is going to sit in the base directory (just like Django's), and will aid us with migrations.
Consider the following code and plug it into your manage.py
file
Remember, if you come back to the gitbook later on, or have had to restart your PC, you will lose your environment variables. This means that your computer has forgotten what your DATABASE_URL
is and you will need to re-add them by following the steps in 02 - Chief Configuration
In this file, we are creating utilizing Flask-Script and Flask-Migrate. These are Flask Extensions made to give us some great quality of life features. One of these features is automatic migrations. On line 11 in manage.py
, we are creating a new Migrate
object that takes in an app
and a db
object. Remember db.Model
? This is where Flask-Migrate will look for changes once we give the command, and then migrate them to the database. Line 15 is where we define what the command will be. We are adding a db
command that comes from the MigrateCommand
object in Flask-Migrate. This will also give us extra options such as db init
, db migrate
, and db upgrade
.
To perform the migrations (we have our initial models), cd into the folder that contains manage.py
, and use the following commands in your terminal
python manage.py db init
- reads changes to db and creates a migrations folder
python manage.py db migrate
- applies migrations to database
python manage.py db upgrade
- commits all changes to the database