migrations#
What is migrations/ in Django?#
- Short definition:
Migrations are Djangoβs way of tracking and applying database schema changes.
- In simple words:
Migrations convert your Python models into database tables.
- Summary:
Django migrations are Python files that track and apply database schema changes based on your models.
Where migrations/ fits in Django#
Flow:
models.py β migrations β database (db.sqlite3 / PostgreSQL)
Django never directly changes the database.
It always uses migrations.
Structure of migrations/ folder#
Inside your app:
myapp/
βββ migrations/
β βββ __init__.py
β βββ 0001_initial.py β appears later
Why this is a folder?#
Because Django stores multiple migration files over time.
__init__.py inside migrations#
This file:
# empty
- Purpose:
Marks migrations as a Python package
Allows Django to import migration files
You never touch this file
When are migration files created?#
- Migration files are created when:
You write or change models
You run:
python manage.py makemigrations
Example:
# myapp/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
Run:
python manage.py makemigrations
Django creates:
migrations/
βββ 0001_initial.py
What is inside a migration file?#
Example 0001_initial.py:
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(primary_key=True)),
('title', models.CharField(max_length=200)),
('content', models.TextField()),
],
),
]
- This means:
Create table post
Add columns title, content
Add primary key id
Two-step migration process#
Step1: Create migration files
python manage.py makemigrations
Creates files in migrations/
Step2: Apply migrations to database
python manage.py migrate
β‘οΈ Updates db.sqlite3 (or PostgreSQL)
Migration commands you must know#
Command |
Purpose |
|---|---|
makemigrations |
Create migration files |
migrate |
Apply migrations |
showmigrations |
List applied/unapplied migrations |
sqlmigrate app 0001 |
Show SQL |
migrate app zero |
Rollback app |
Example real workflow
# change models.py
python manage.py makemigrations
python manage.py migrate
Repeat this every time you change models.
- Common beginner mistakes:
Editing database manually
Forgetting to run migrate
Deleting migration files randomly
Running migrate without makemigrations
Using migrations as version control
Is it safe to delete migrations?
Situation |
Safe? |
|---|---|
Early development |
β οΈ Sometimes |
Production |
β NEVER |
Team project |
β NEVER |
Reset local DB |
β οΈ Carefully |
- Why migrations are powerful:
Database versioning
Rollbacks
Team synchronization
Database-agnostic (SQLite β PostgreSQL β MySQL)
Production safe