As your Django project evolves, your database structure also needs to change. You may add new fields, modify existing ones, or create new models. Django handles these changes using a powerful system called migrations.
In this tutorial, you will learn what migrations are, why they are important, how to create and apply them, and how Django keeps your database synchronized with your models.
A migration is a file that describes changes to your database structure.
When you modify your models, Django does not automatically update the database. Instead, it creates migration files that define what should change.
If you add a new field:
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.BooleanField(default=True)Django generates a migration that says:
➡️ “Add a new column published to the Post table”
Migrations allow you to:
Without migrations, managing database updates would be complex and error-prone.
Django migrations follow a simple workflow:
After changing your models, run:
python manage.py makemigrationsblog/migrations/0001_initial.py
0002_add_published_field.pyThese files contain instructions for modifying the database.
To apply changes to the database, run:
python manage.py migrateclass Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.BooleanField(default=True)python manage.py makemigrationspython manage.py migrateNow your database includes the new column.
To see which migrations are applied:
python manage.py showmigrationsblog
[X] 0001_initial
[X] 0002_add_field[X] = applied[ ] = not appliedMigration files are Python files that describe database operations.
operations = [
migrations.AddField(
model_name='post',
name='published',
field=models.BooleanField(default=True),
),
]You usually do not need to edit these manually.
You can undo migrations if needed.
python manage.py migrate blog 0001This will revert the database to migration 0001.
python manage.py migrate blogThis applies migrations only for the blog app.
Sometimes during development, you may want to reset migrations.
Steps:
__init__.py)db.sqlite3)python manage.py makemigrations
python manage.py migrate
⚠️ Only do this in development, not in production.
Django migrations are automatic, but they rely on detecting model changes.
If you rename a field:
title → nameDjango may treat it as:
titlenameThis may lead to data loss if not handled carefully.
Migrations can:
CreateModel)AddField)RemoveField)RenameField)AlterField)DeleteModel)Migration files are ordered and depend on each other.
Example:
0001_initial → 0002_add_field → 0003_update_modelDjango applies them in order to maintain consistency.
Migration files should be committed to version control.
Changing models without running:
makemigrations
migratewill cause errors.
This can break your database history.
Only do this if you understand what you are doing.
Leads to inconsistent databases in teams.
makemigrations → creates filesmigrate → applies changes# Step 1: Modify models
# Step 2:
python manage.py makemigrations# Step 3:
python manage.py migrate# Step 4:
python manage.py runserverThis is the standard workflow in Django development.
Think of migrations like a version history for your database.
Each migration is like a renovation plan applied step by step.
In this tutorial, you learned how Django migrations manage database changes. You saw how to create migrations, apply them, view their status, and roll them back. Migrations are essential for keeping your database synchronized with your models.
makemigrations creates migration filesmigrate applies themmigrations/makemigrations do?migrate do?Migrations are a core part of Django development. They allow you to safely evolve your database structure over time without writing raw SQL. Once you master migrations, you can confidently manage changes in real-world projects.