Django Migrations Basics

  • Learn the basics of Django migrations and how to manage database schema changes.
  • Why Migrations Are Important?

    In Django:

    • Models = Database structure

    • Migrations = Database changes

    Migrations keep Python models and database tables in sync.

    Without migrations:

    • Tables won’t be created

    • Data structure won’t update

    • Applications will break

    What Are Migrations?

    Migrations are Django’s way of:

    • Tracking model changes

    • Applying them to the database safely

    • Maintaining version history of the database

    Simple Definition:

    Migration = Instructions to update the database

    Model → Migration → Database Flow

    models.py

       ↓

    makemigrations

       ↓

    migrate

       ↓

    Database Tables

    1. makemigrations

    What is makemigrations?

    makemigrations:

    • Detects changes in models.py

    • Creates migration files

    • Does NOT update the database

    👉 Think of it as drafting database changes

    When to Use?

    • After creating a model

    • After modifying a model field

    Code Example: Create Migration File

Creating Migration Files

Converts model changes into migration instructions

python manage.py makemigrations

  • Output Example

    Migrations for 'students':

      students/migrations/0001_initial.py

    ✔ Migration file created
    ❌ Database not changed yet

    Inside Migration File (Auto-Generated)

    class Migration(migrations.Migration):

        operations = [

            migrations.CreateModel(

                name='Student',

                fields=[

                    ('name', models.CharField(max_length=100)),

                ],

            ),

        ]

    2. migrate

    What is migrate?

    migrate:

    • Applies migration files to the database

    • Creates or updates tables

    • Executes SQL behind the scenes

    👉 This command actually changes the database

    When to Use?

    • After makemigrations

    • When setting up a new project

    • After pulling new code from repository

    Code Example: Apply Migrations

Applying Migrations

Creates or updates database tables

python manage.py migrate
  • What Happens Internally?

    • Django checks unapplied migrations

    • Executes SQL queries

    • Updates database schema

    3. showmigrations

    What is showmigrations?

    showmigrations:

    • Displays all migration files

    • Shows applied & unapplied migrations

    • Helps in debugging database issues

    Symbols Meaning:

    • [X] → Migration applied

    • [ ] → Migration not applied

    Code Example: View Migration Status

Viewing Migration Status

Shows migration history

python manage.py showmigrations	
  • Sample Output

    students

     [X] 0001_initial

     [ ] 0002_add_age

    ✔ First migration applied
    ❌ Second migration pending

    4. Real-World Example

Step 1: Create Model

class Student(models.Model):
    name = models.CharField(max_length=100)

Step 2: Create Migration

python manage.py makemigrations

Step 3: Apply Migration

python manage.py migrate
  • ✔ Database table created successfully

    5. Common Beginner Mistakes

    Mistake

    Reason

    Solution

    Table not created

    Forgot migrate

    Run migrate

    No migration file

    Forgot makemigrations

    Run makemigrations

    App not listed

    App not registered

    Add to INSTALLED_APPS