← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 3 of 8

The migrate Command

migrate takes all the written-down migration instructions and actually builds or updates the real database tables.

Applying All Pending Migrations

Running migrate with no arguments applies every migration that hasn't been applied yet, across every installed app, in the correct dependency order.

Example: Applying All Pending Migrations

Django prints 'Applying blog.0002_add_published_flag... OK' for each migration it runs against the database.

bash
python manage.py migrate

⚠️ Run this command in your terminal.

Migrating a Single App

Passing an app name after migrate applies only that app's pending migrations, leaving the rest of the project untouched.

Example: Migrating a Single App

Only migrations belonging to the blog app are applied; other apps' pending migrations stay unapplied.

bash
python manage.py migrate blog

⚠️ Run this command in your terminal.

How Django Tracks Applied Migrations

Django keeps a table called django_migrations that records the app and migration name for everything it has already applied, so running migrate again is always safe and won't reapply old changes.

Note: Running migrate repeatedly is harmless — it always checks this table first.

Example: How Django Tracks Applied Migrations

This queries Django's own migration-tracking table to list which blog migrations have already been recorded as applied.

bash
python manage.py shell
>>> from django.db.migrations.recorder import MigrationRecorder
>>> MigrationRecorder.Migration.objects.filter(app='blog')

⚠️ Run this command in your terminal.

Rolling Back a Migration

Passing a specific migration name after the app rolls the database back to that point, reversing every migration applied after it.

Warning: Reversing migrations that already dropped data (like removing a column) can permanently lose that data.

Example: Rolling Back a Migration

This reverts the blog app back to the state it was in right after migration 0001, undoing 0002 and anything after it.

bash
python manage.py migrate blog 0001

⚠️ Run this command in your terminal.

Common Mistakes
  1. Running migrate without ever running makemigrations first, then wondering why the database doesn't reflect a recent model change.
  2. Forgetting to run migrate after pulling a teammate's new migration files from version control.
  3. Assuming migrate re-runs every migration every time, when it only applies ones that haven't been applied yet.
Chapter Summary
  • migrate applies pending migrations to the actual database.
  • Django tracks which migrations have already run in a special django_migrations table.
  • Running migrate again after nothing has changed does nothing — it's safe to run often.
  • migrate also sets up Django's own built-in tables like auth and sessions the first time it runs.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.