📝 Django

Django Migrations

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
82
🌱
Level
Beginner

A migration is a file describing changes to the database schema. Django tracks the state of your models and generates SQL automatically.

Workflow

# 1. Change a model in models.py
# 2. Create the migration file
python manage.py makemigrations

# 3. Apply it to the database
python manage.py migrate

makemigrations — creating a file

python manage.py makemigrations          # for all applications
python manage.py makemigrations tasks    # only for tasks
python manage.py makemigrations --name add_priority  # with a custom name

Creates a file like tasks/migrations/0002_task_priority.py:

from django.db import migrations, models

class Migration(migrations.Migration):
    dependencies = [('tasks', '0001_initial')]

    operations = [
        migrations.AddField(
            model_name='task',
            name='priority',
            field=models.IntegerField(default=0),
        ),
    ]

migrate — applying migrations

python manage.py migrate                    # apply all
python manage.py migrate tasks              # only for one app
python manage.py migrate tasks 0001         # roll back to a version
python manage.py migrate tasks zero         # roll back everything

Migration status

python manage.py showmigrations
# [X] tasks.0001_initial          ← applied
# [X] tasks.0002_task_priority
# [ ] tasks.0003_task_due_date    ← not yet applied

What NOT to do

# ❌ Do not delete migration files manually
# ❌ Do not edit already-applied migrations
# ❌ Do not name fields with reserved SQL keywords

squashmigrations — merging migrations

# Merge migrations 0001-0010 into one
python manage.py squashmigrations tasks 0001 0010

Useful when many migrations have accumulated. After squashing, the old files can be deleted once all environments have been updated.

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 132
📝

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

📅 30.06.2026 👁️ 100
📝

RAG: Chatting with Documents via Vector Search

RAG (Retrieval-Augmented Generation) is a pattern for working with your own documents. Instead of fine-tuning...

📅 30.06.2026 👁️ 89

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!