0Pricing
Flask Academy · Lesson

Init and Autogenerate a Migration

Run flask db init and migrate.

Init and Autogenerate a Migration is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Install Flask-Migrate

First add the package to your project. Flask-Migrate brings Alembic and a set of handy flask db commands. 📦

pip install Flask-Migrate

Wire It to the App

Create a Migrate object and bind it to your app and db. That single line activates the whole flask db command group.

from flask_migrate import Migrate
migrate = Migrate(app, db)

Initialize the Repo

Run flask db init once per project. It scaffolds a migrations folder that will hold every version script you generate.

flask db init

Inside the Folder

The new folder holds an alembic.ini, an env.py, and an empty versions directory where future migration files will live.

Autogenerate a Migration

flask db migrate compares your models to the live schema and writes a script describing exactly what changed.

flask db migrate -m "create users table"

The -m Message

Always pass a clear -m message. It labels the migration so your team can read the history like a changelog later.

A New Versions File

Each migrate command drops a fresh file in versions, named with a random revision id plus your message slug.

Models Must Be Imported

Autogenerate only sees models that are actually imported when the command runs. Missing imports mean missing tables in the script.

It Detects Differences

Alembic diffs the model metadata against the database. It catches new tables, added columns, and many changed constraints automatically.

Migrate Does Not Apply

Key point: migrate only writes the script. Your database is still untouched until you actually apply it in the next lesson.

Commit the Migration

Treat migration files as code and commit them to git. Teammates replay the exact same changes from your version history.

Quick Check

You ran flask db migrate and saw a new file appear. Did your tables change?

Recap

You wired up Migrate, ran flask db init, and autogenerated a script with flask db migrate ready to apply. 🎉

Frequently asked questions

Is the “Init and Autogenerate a Migration” lesson free?

Yes — the full text of “Init and Autogenerate a Migration” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Init and Autogenerate a Migration”?

Run flask db init and migrate. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Init and Autogenerate a Migration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why You Need Migrations, Not create_all
  2. Init and Autogenerate a Migration
  3. Apply and Roll Back with upgrade
  4. Review and Edit Generated Scripts
← Back to Flask Academy