Bird
Raised Fist0
Djangoframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Django migrations?
easy
A. To keep the database schema synchronized with Django models
B. To create HTML templates automatically
C. To manage user authentication
D. To optimize Django views for speed

Solution

  1. Step 1: Understand what migrations do

    Django migrations track changes in models and update the database schema accordingly.
  2. Step 2: Compare options with this purpose

    Only To keep the database schema synchronized with Django models describes keeping the database schema in sync with models, which is the core purpose of migrations.
  3. Final Answer:

    To keep the database schema synchronized with Django models -> Option A
  4. Quick Check:

    Migrations purpose = sync DB schema [OK]
Hint: Migrations sync models and database structure [OK]
Common Mistakes:
  • Confusing migrations with template or view management
  • Thinking migrations handle user authentication
  • Assuming migrations optimize performance
2. Which command do you run to create new migration files after changing models?
easy
A. python manage.py migrate
B. python manage.py makemigrations
C. python manage.py runserver
D. python manage.py collectstatic

Solution

  1. Step 1: Identify the command for creating migration files

    The command makemigrations generates migration files based on model changes.
  2. Step 2: Differentiate from other commands

    migrate applies migrations, runserver starts the server, and collectstatic collects static files.
  3. Final Answer:

    python manage.py makemigrations -> Option B
  4. Quick Check:

    Create migrations = makemigrations [OK]
Hint: Use makemigrations to create migration files [OK]
Common Mistakes:
  • Using migrate instead of makemigrations to create files
  • Confusing runserver with migration commands
  • Thinking collectstatic manages migrations
3. Given this sequence of commands:
python manage.py makemigrations
python manage.py migrate

What happens after running these commands?
medium
A. The server starts running with updated models
B. Static files are collected and served
C. Migration files are created and then applied to update the database schema
D. The database is reset to initial state

Solution

  1. Step 1: Understand makemigrations effect

    This command creates migration files reflecting model changes but does not apply them yet.
  2. Step 2: Understand migrate effect

    This command applies the created migration files to update the database schema accordingly.
  3. Final Answer:

    Migration files are created and then applied to update the database schema -> Option C
  4. Quick Check:

    makemigrations + migrate = create & apply migrations [OK]
Hint: makemigrations creates files; migrate applies them [OK]
Common Mistakes:
  • Thinking migrate creates migration files
  • Assuming these commands start the server
  • Confusing static file commands with migrations
4. You ran python manage.py migrate but got an error saying a migration file is missing. What is the likely cause?
medium
A. You forgot to run makemigrations to create migration files
B. You ran migrate before starting the server
C. Your models.py file is empty
D. You need to delete the database before migrating

Solution

  1. Step 1: Analyze the error about missing migration files

    This usually means migration files were not created yet for model changes.
  2. Step 2: Identify the missing step

    Running makemigrations generates migration files needed before applying them with migrate.
  3. Final Answer:

    You forgot to run makemigrations to create migration files -> Option A
  4. Quick Check:

    Missing migration files = forgot makemigrations [OK]
Hint: Always run makemigrations before migrate [OK]
Common Mistakes:
  • Thinking migrate creates migration files
  • Assuming server must run before migrate
  • Believing database must be deleted before migrating
5. You added a new field to a model and want to share the changes with your team. What is the correct workflow to update the database and share the changes safely?
hard
A. Delete the database, then run makemigrations and migrate on each machine
B. Run migrate first, then create migration files manually and share them
C. Only update the models.py file and ask team members to restart their servers
D. Run makemigrations, commit migration files to version control, then run migrate on all machines

Solution

  1. Step 1: Create migration files after model changes

    Run makemigrations to generate migration files reflecting the new field.
  2. Step 2: Share migration files and apply them

    Commit migration files to version control so the team can get them, then run migrate on each machine to update the database schema safely.
  3. Final Answer:

    Run makemigrations, commit migration files to version control, then run migrate on all machines -> Option D
  4. Quick Check:

    makemigrations + commit + migrate = safe team update [OK]
Hint: Commit migration files before migrating on other machines [OK]
Common Mistakes:
  • Running migrate before creating migration files
  • Deleting databases unnecessarily
  • Only updating models.py without migrations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard