๐Ÿ“ Django

Django Migrations

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
286
๐ŸŒฑ
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

๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐Ÿ“

CSRF Protection in Django

CSRF (Cross-Site Request Forgery) is an attack in which an adversary tricks the user's browser...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 294
๐Ÿ“

Django Development Server

Django's built-in server is intended for local development: it runs the project and reloads Python...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 288
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum