/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
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 pollsOutput
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_initialThe 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
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 0001Output
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
| Command | What it does |
|---|---|
| makemigrations [app] | write migration files for model changes |
| makemigrations --check --dry-run | fail (exit 1) if migrations are missing: useful in CI |
| migrate [app] [migration] | apply, or roll back to a given migration |
| migrate app zero | unapply every migration of an app |
| showmigrations | list migrations, [X] when applied |
| sqlmigrate app 0001 | print the SQL of one migration |
| squashmigrations app 0004 | merge old migrations into one |
Sources
Related pages
cheat sheets · djangoDjango manage.py cheat sheet: project, migrations, users, testingDjango manage.py cheat sheet: startproject and startapp, makemigrations and migrate, createsuperuser, runserver, shell, check, test, collectstatic and dumpdata.glossary · sqlalchemyWhat is an ORM (object-relational mapper)?An ORM maps database tables to classes and rows to objects, so you query and save data with code instead of SQL strings. A SQLAlchemy 2 example and trade-offs.errors · djangoImproperlyConfigured: Requested setting …, but settings are not configuredDjango raises ImproperlyConfigured when code reads settings before DJANGO_SETTINGS_MODULE is set or settings.configure() runs. Set it, then call django.setup().