Skip to content

/commands/django/makemigrations-migrate

python manage.py makemigrations and migrate: create and apply migrations

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
tool
Django
versions
Django 5.2, Django 6.x
os
any

python manage.py makemigrations compares your models with the existing migration files and writes new migrations for the differences. python manage.py migrate applies the migrations that have not run yet to the database. Run makemigrations after every model change, commit the generated files, and run migrate everywhere the code is deployed.

From a new model to a table

bash
django-admin startproject mysite . >/dev/null
python manage.py startapp polls
cat > polls/models.py <<'PY'
from django.db import models

class Question(models.Model):
    text = models.CharField(max_length=200)
    published = models.DateTimeField()
PY
echo 'INSTALLED_APPS += ["polls"]' >> mysite/settings.py
python manage.py makemigrations polls
python manage.py migrate polls
python manage.py showmigrations polls

Output

Migrations for 'polls':
  polls\migrations\0001_initial.py
    + Create model Question
Operations to perform:
  Apply all migrations: polls
Running migrations:
  Applying polls.0001_initial... OK
polls
 [X] 0001_initial

The migration path is printed with backslashes because this page was verified on Windows; on Linux and macOS it reads polls/migrations/0001_initial.py.

See the SQL before applying it

bash
django-admin startproject mysite . >/dev/null
python manage.py startapp polls
printf 'from django.db import models\n\nclass Question(models.Model):\n    text = models.CharField(max_length=200)\n' > polls/models.py
echo 'INSTALLED_APPS += ["polls"]' >> mysite/settings.py
python manage.py makemigrations polls >/dev/null
python manage.py sqlmigrate polls 0001

Output

BEGIN;
--
-- Create model Question
--
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(200) NOT NULL);
COMMIT;

Commands you will use

CommandWhat it does
makemigrations [app]write migration files for model changes
makemigrations --check --dry-runfail (exit 1) if migrations are missing: useful in CI
migrate [app] [migration]apply, or roll back to a given migration
migrate app zerounapply every migration of an app
showmigrationslist migrations, [X] when applied
sqlmigrate app 0001print the SQL of one migration
squashmigrations app 0004merge old migrations into one

Sources