Python - 301 - Flask
  • 00 - Appealing API's
  • 01- A Smooth Start
  • 02 - Chief Configuration
  • 03 - Example Endpoint and an Adornable App
  • 04 - Modeling the Models
  • 05 - Scheming Schemas
  • 06 - Making Migrations
  • 07 - Uniformed Users
  • 08 - Alarming Authentication
  • 09 - Account Actions
  • 10 - Postman Prevalence and Examining Endpoints
  • 11 - Blog Post Blogging
Powered by GitBook
On this page

Was this helpful?

06 - Making Migrations

In this module we will look at migrations, what they are, and connecting to our database for the first time

Previous05 - Scheming SchemasNext07 - Uniformed Users

Last updated 6 years ago

Was this helpful?

Creating our own Manager Script

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

If at any time you get an error, check your database URL. These can be a bit tricky to debug because everyone has different PostgreSQL install. A great majority of these errors will come down to a faulty database URL