0
0
Djangoframework~10 mins

Migrations concept and workflow in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Migrations concept and workflow
Write/Change Models
Run makemigrations
Create Migration Files
Run migrate
Apply Changes to Database
Database Updated
Ready for Next Change
This flow shows how changes in Django models lead to migration files, which then update the database schema.
Execution Sample
Django
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)

# Run: python manage.py makemigrations
# Run: python manage.py migrate
Defines a model and runs commands to create and apply migrations updating the database.
Execution Table
StepActionInput/CommandOutput/Result
1Write model codeDefine Book model with title fieldModel code saved in models.py
2Run makemigrationspython manage.py makemigrationsMigration file created in migrations/ folder
3Inspect migration fileLook at generated migrationShows CreateModel operation for Book
4Run migratepython manage.py migrateMigration applied, database table created
5Check databaseInspect DB schemaNew table for Book exists with title column
6Modify modelAdd author fieldModel code updated
7Run makemigrationspython manage.py makemigrationsNew migration file created for adding author
8Run migratepython manage.py migrateDatabase schema updated with author column
9Check databaseInspect DB schemaBook table now has title and author columns
10ExitNo more changesDatabase schema matches models
💡 No more model changes, migrations and database are synchronized
Variable Tracker
VariableStartAfter Step 1After Step 6After Step 8Final
models.pyempty or old modelBook model with titleBook model with title and authorSame as after step 6Same as after step 6
migration filesnoneinitial migration for Bookinitial migration for Bookinitial migration + new migration for authorinitial migration + new migration for author
database schemaold schemaold schemaBook table with titleBook table with title and authorBook table with title and author
Key Moments - 3 Insights
Why do we need to run makemigrations before migrate?
makemigrations creates migration files from model changes (see step 2 and 7), migrate applies those files to the database (steps 4 and 8). Without makemigrations, migrate has no instructions.
What happens if we change models but forget to run migrate?
The database schema stays old (see step 6 vs step 9). The app code and database get out of sync, causing errors.
Can we edit migration files manually?
Yes, but carefully. Migration files are Python code describing DB changes (step 3). Editing is advanced and usually not needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is created after running 'makemigrations' the first time?
AThe updated models.py file
BThe database table for Book
CA migration file describing the Book model creation
DThe author field added to Book
💡 Hint
Check step 2 in the execution table where makemigrations creates migration files
At which step does the database schema first include the author column?
AStep 8
BStep 4
CStep 6
DStep 2
💡 Hint
Look at step 8 where migrate applies the new migration adding author
If you skip running 'makemigrations' after changing models, what happens when you run 'migrate'?
AMigrate applies the changes anyway
BMigrate does nothing because no migration files exist
CMigrate deletes the database tables
DMigrate updates models.py automatically
💡 Hint
Refer to key moment 1 and steps 2 and 4 in the execution table
Concept Snapshot
Django migrations track model changes.
Run 'makemigrations' to create migration files.
Run 'migrate' to apply changes to the database.
Keep models.py, migration files, and DB schema in sync.
Migrations allow safe, repeatable DB updates.
Full Transcript
Django migrations help update the database when you change your models. First, you write or change your model code. Then you run 'python manage.py makemigrations' which creates migration files describing those changes. Next, you run 'python manage.py migrate' to apply those changes to the database schema. This process keeps your database structure in sync with your Python code. If you add a new field to a model, you must run makemigrations to create a new migration file, then migrate to update the database. Skipping makemigrations means migrate has no instructions and does nothing. Migration files are Python scripts stored in your app's migrations folder. They can be inspected or edited carefully. This workflow ensures your app and database evolve together safely.