0
0
Djangoframework~10 mins

makemigrations and migrate commands in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - makemigrations and migrate commands
Write or change models.py
Run: python manage.py makemigrations
Create migration files
Run: python manage.py migrate
Apply migrations to database
Database schema updated
This flow shows how changes in Django models create migration files, which are then applied to update the database schema.
Execution Sample
Django
python manage.py makemigrations
python manage.py migrate
These commands create migration files from model changes and apply them to the database.
Execution Table
StepCommandActionResult
1makemigrationsDetect changes in models.pyCreates new migration file(s)
2makemigrationsNo changes detectedNo new migration files created
3migrateApply all unapplied migrationsDatabase schema updated
4migrateNo unapplied migrationsNo changes to database
5ExitAll migrations appliedDatabase is up to date
💡 No more migrations to apply, database schema matches models
Variable Tracker
VariableStartAfter makemigrations 1After migrate 1Final
migration_files[][0001_initial.py][0001_initial.py][0001_initial.py]
database_schemaemptyemptyupdated with new tablesupdated with new tables
models.pydefined modelsdefined modelsdefined modelsdefined models
Key Moments - 3 Insights
Why does running 'makemigrations' sometimes create no new files?
Because no changes were made to models.py since the last migration, as shown in execution_table step 2.
What happens if you run 'migrate' without any new migration files?
No changes are applied to the database since all migrations are already applied, as shown in execution_table step 4.
Why do we need to run both 'makemigrations' and 'migrate'?
'makemigrations' creates migration files from model changes, and 'migrate' applies those files to update the database, shown in steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is created after running 'makemigrations' when models change?
ADatabase schema updated
BNew migration file(s)
CNo changes
DModels.py file
💡 Hint
Refer to execution_table row 1 where makemigrations creates migration files
At which step does the database schema get updated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 where migrate applies migrations
If you change models.py but forget to run 'makemigrations', what happens when you run 'migrate'?
ANo migration files to apply, so no database change
BDatabase updates with new schema
CError occurs
DNew migration files are created automatically
💡 Hint
Check variable_tracker and execution_table steps 1 and 3 for migration file creation and application
Concept Snapshot
Django migrations update your database schema.
Run 'makemigrations' to create migration files from model changes.
Run 'migrate' to apply these files to the database.
No model changes means no new migration files.
Migrations keep your database and models in sync.
Full Transcript
In Django, when you change your models.py file, you need to update your database schema to match. First, you run 'python manage.py makemigrations' which looks at your models and creates migration files describing the changes. If no changes are found, no new files are created. Next, you run 'python manage.py migrate' which applies all pending migration files to the database, updating its structure. If there are no new migrations, the database stays the same. This two-step process ensures your database matches your code models.