← Back to Django, page 1

django-0004 · Easy fresher

What are Django migrations, and what is the difference between makemigrations and migrate?

Migrations are Python files that record changes to models so the database schema can follow the code. makemigrations compares models with the last migration state and writes new migration files. migrate applies unapplied migration files to the database and records them in the django_migrations table.

Full answer

A migration is a Python file inside an app's migrations/ folder. It lists operations such as CreateModel, AddField or AlterField. Migrations let the database schema change in step with the code, and let every developer and server reach the same schema.

Two commands do different jobs:

  • python manage.py makemigrations reads your models, compares them with the state built from existing migration files, and writes a new file such as 0002_student_roll_no.py. It does not touch the database.
  • python manage.py migrate reads migration files, checks the django_migrations table to see which ones already ran, and applies the rest in dependency order.

Other useful commands:

  • showmigrations lists migrations with [X] for applied ones.
  • sqlmigrate app 0002 prints the SQL a migration will run. Useful for review before production.
  • migrate app 0001 moves an app back to an earlier migration (if the operations are reversible).
  • makemigrations --check --dry-run fails if models changed without a migration. Good for CI.

Migration files are code. Commit them to version control, review them, and never edit an applied migration on a shared database. If two developers create 0003 on different branches, Django detects the conflict and makemigrations --merge creates a merge migration.

Adding a field without a default to a table that already has rows forces Django to ask for a one-off default, because existing rows need a value.

python
import os
import sys
import tempfile
from pathlib import Path

# Build a tiny app on disk so makemigrations has somewhere to write
root = Path(tempfile.mkdtemp())
app = root / "attendance"
(app / "migrations").mkdir(parents=True)
(app / "__init__.py").write_text("")
(app / "migrations" / "__init__.py").write_text("")
(app / "models.py").write_text(
    "from django.db import models\n\n"
    "class Attendance(models.Model):\n"
    "    roll_no = models.IntegerField()\n"
    "    present = models.BooleanField(default=True)\n"
)
sys.path.insert(0, str(root))
os.chdir(root)

import django
from django.conf import settings

settings.configure(
    INSTALLED_APPS=["attendance"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
    DEFAULT_AUTO_FIELD="django.db.models.BigAutoField",
)
django.setup()

from django.core.management import call_command

call_command("makemigrations", "attendance", verbosity=0)
print(sorted(p.name for p in (app / "migrations").glob("0*.py")))

call_command("showmigrations", "attendance")
call_command("migrate", "attendance", verbosity=0)
call_command("showmigrations", "attendance")

Standalone script. It writes a tiny app to a temporary folder, then calls makemigrations, showmigrations and migrate the same way manage.py would. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): ['0001_initial.py'] attendance [ ] 0001_initial attendance [X] 0001_initial

Key points

  • makemigrations writes files and does not touch the database
  • migrate runs unapplied files and records them in django_migrations
  • Migration files belong in version control
  • sqlmigrate shows the SQL before you run it
  • makemigrations --check catches missing migrations in CI

Common mistakes

  • Running migrate and expecting it to notice model changes without makemigrations
  • Deleting or editing migration files that already ran on production
  • Not committing migration files, so each server generates different ones
  • Ignoring a migration conflict instead of merging it

Follow-ups

  • How do you write a migration that changes data, not schema?
  • How would you roll back a bad migration?
  • What happens when two branches both add migration 0005?

Tips

  • Say clearly which command touches the database
  • Mention sqlmigrate for reviewing SQL
  • Add the CI check with --check to show production habits