"Mastering Flask Migrations: A Step-by-Step Tutorial"

Mastering Flask Migrations: A Comprehensive Tutorial

In the dynamic world of web development, it's crucial to manage changes to your database schema efficiently. Flask-Migrate, an extension for Flask, allows you to handle database migrations with ease. In this tutorial, we'll guide you through the process of using Flask-Migrate to manage your database schema changes.

Prerequisites

Before we dive into the tutorial, ensure you have the following prerequisites:

  • Python 3.6 or later
  • Flask 1.1 or later
  • SQLAlchemy 1.3 or later
  • Flask-Migrate 2.5 or later

Setting Up Your Flask Application

First, let's set up a basic Flask application with SQLAlchemy as the Object-Relational Mapping (ORM) tool. We'll use Flask-Migrate to manage the database migrations.

Python Flask Tutorial 5 - Database with Flask-SQLAlchemy
Python Flask Tutorial 5 - Database with Flask-SQLAlchemy

Install the required packages:

pip install flask flask_sqlalchemy flask_migrate

Create a new file app.py and add the following code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)

Defining Your Database Model

Let's define a simple User model for demonstration purposes.

a black and white poster with the words flask vs fastapp
a black and white poster with the words flask vs fastapp

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

Initializing Flask-Migrate

Now, let's initialize Flask-Migrate. Run the following command in your terminal:

export FLASK_APP=app.py
flask db init

This command creates a new migrations folder and sets up the necessary files.

Generating Migrations

Whenever you make changes to your models, you need to generate a new migration. Let's add a new column active to our User model.

an image of the inside of a glass bottle with labels on it and labeled in english
an image of the inside of a glass bottle with labels on it and labeled in english

active = db.Column(db.Boolean, default=False)

Generate a new migration with the following command:

flask db migrate -m "Add active column to User model"

The -m flag allows you to add a message describing the migration.

Upgrading and Downgrading Migrations

To apply the migration, use the upgrade command:

flask db upgrade

If you need to revert a migration, use the downgrade command:

flask db downgrade

Stamping and Revising Migrations

Sometimes, you might want to mark a specific version as the current one (stamping) or revise the migration history (revising).

Command Description
flask db stamp Stamps the current version as the latest one.
flask db revise -m "Revision message" Revises the migration history with the given message.

Conclusion

In this tutorial, we've covered the basics of using Flask-Migrate to manage database migrations in your Flask applications. By following these steps, you can efficiently handle schema changes and keep your database in sync with your application's models.

Learn About Python Microservices by Building an App Using Django, Flask, and React
Learn About Python Microservices by Building an App Using Django, Flask, and React
Python Website Full Tutorial - Flask, Authentication, Databases & More
Python Website Full Tutorial - Flask, Authentication, Databases & More
How to Make  a Medieval Leather Flask (FREE Pattern in Description)
How to Make a Medieval Leather Flask (FREE Pattern in Description)
How to build a web app using Python’s Flask and Google App Engine
How to build a web app using Python’s Flask and Google App Engine
Splash Bottle, Bottle Spilling, Water Bottle Splash Effect, Mineral Water Bottle Splash Advertisement, Water Splash Bottle, Creative Water Bottle Poster, Crushed Up Water Bottle, Bottle Creative Photography, Crushed Water Bottle
Splash Bottle, Bottle Spilling, Water Bottle Splash Effect, Mineral Water Bottle Splash Advertisement, Water Splash Bottle, Creative Water Bottle Poster, Crushed Up Water Bottle, Bottle Creative Photography, Crushed Water Bottle
a black and white drawing of a flask
a black and white drawing of a flask
a drawing of a glass flask filled with liquid
a drawing of a glass flask filled with liquid
Gunpowder flask Gunpowder Flask, 17th Century, Flask
Gunpowder flask Gunpowder Flask, 17th Century, Flask
Hand-painted steel whiskey flask.
Hand-painted steel whiskey flask.
a flask covered in lots of different colored stones and beads with the words,'turn it up '
a flask covered in lots of different colored stones and beads with the words,'turn it up '
a flask shaped like a human heart
a flask shaped like a human heart
a woman's hand holding a black flask with skulls on it
a woman's hand holding a black flask with skulls on it
Erlenmeyer Flask With Liquid Sticker
Erlenmeyer Flask With Liquid Sticker
Floral bitch don't kill my vibe Kendrick Lamar flask Flask Designs Diy, Flask Painting Ideas, Frat Coolers Ideas Kappa Sigma, Rap Painted Coolers, Painted Flask Ideas, Sorority Flask Ideas, Kendrick Lamar Dont Kill My Vibe, Fraternity Flask Painted, Painted Flask Fraternity
Floral bitch don't kill my vibe Kendrick Lamar flask Flask Designs Diy, Flask Painting Ideas, Frat Coolers Ideas Kappa Sigma, Rap Painted Coolers, Painted Flask Ideas, Sorority Flask Ideas, Kendrick Lamar Dont Kill My Vibe, Fraternity Flask Painted, Painted Flask Fraternity
a red and white flask with the words first aid on it sitting on a carpet
a red and white flask with the words first aid on it sitting on a carpet
Comparing FastAPI and Flask for RestAPI Development in Python
Comparing FastAPI and Flask for RestAPI Development in Python
Painted Galaxy Hydroflask💫🪐
Painted Galaxy Hydroflask💫🪐
5.6 Heat Transfer Methods – Conduction, Convection and Radiation Introduction – Douglas College Physics 1207
5.6 Heat Transfer Methods – Conduction, Convection and Radiation Introduction – Douglas College Physics 1207
a person holding a flask with the word cheers on it in front of a map
a person holding a flask with the word cheers on it in front of a map
a flask with a skull and crossbones on it
a flask with a skull and crossbones on it
a white hydro flask bottle sitting on top of a table
a white hydro flask bottle sitting on top of a table
Building a flask
Building a flask
How to Make a Pewter Hip Flask
How to Make a Pewter Hip Flask