These commands help you save and apply changes to your database structure easily.
makemigrations and migrate commands in Django
python manage.py makemigrations python manage.py migrate
makemigrations creates files that describe your database changes.
migrate applies those changes to the actual database.
python manage.py makemigrations
python manage.py migrate
python manage.py makemigrations appname
python manage.py migrate appname 0001This example shows a simple model for books. Running makemigrations creates a migration file describing the new table. Running migrate creates the table in the database.
# models.py from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) # Terminal commands # Step 1: Create migration files # python manage.py makemigrations # Step 2: Apply migrations to database # python manage.py migrate
Always run makemigrations after changing models to keep migrations up to date.
Run migrate to apply all migrations before running your app to avoid errors.
You can look inside migration files to see what changes will be applied.
makemigrations creates files describing database changes.
migrate applies those changes to the database.
Use these commands to keep your database structure in sync with your Django models.
Practice
makemigrations command in Django?Solution
Step 1: Understand the role of
This command generates files that describe changes made to Django models, like adding or removing fields.makemigrationsStep 2: Differentiate from
migratemigrateapplies those changes to the database, butmakemigrationsonly creates the description files.Final Answer:
To create migration files that describe changes in models -> Option DQuick Check:
makemigrationscreates files = A [OK]
- Confusing makemigrations with migrate
- Thinking makemigrations changes the database directly
- Assuming makemigrations starts the server
Solution
Step 1: Identify the command to apply migrations
The commandpython manage.py migrateapplies migration files to update the database schema.Step 2: Check other options
makemigrationscreates migration files,runserverstarts the server, andmakemigrateis not a valid command.Final Answer:
python manage.py migrate -> Option BQuick Check:
Apply migrations = migrate = A [OK]
- Typing makemigrate instead of migrate
- Using makemigrations to update the database
- Confusing runserver with migration commands
python manage.py makemigrations
python manage.py migrate
What will happen after running these commands?
Solution
Step 1: Understand the effect of
This command creates migration files describing model changes.makemigrationsStep 2: Understand the effect of
This command applies those migration files to update the database schema accordingly.migrateFinal Answer:
The database schema will be updated to match the current models -> Option AQuick Check:
makemigrations + migrate = update DB schema = C [OK]
- Thinking migrate starts the server
- Believing migration files get deleted automatically
- Assuming models reset after migrations
python manage.py migrate but got an error saying "No migrations to apply." What is the most likely cause?Solution
Step 1: Analyze the error message
"No migrations to apply" means Django found no new migration files to run.Step 2: Identify the missing step
If you did not runmakemigrations, no migration files exist to apply.Final Answer:
You forgot to runmakemigrationsto create migration files -> Option AQuick Check:
No migrations found = forgot makemigrations = D [OK]
- Restarting server instead of creating migrations
- Assuming database corruption without checking migrations
- Ignoring missing migration files
makemigrations. Later, you run python manage.py migrate. What will happen?Solution
Step 1: Understand the role of
This command creates migration files describing model changes like new fields.makemigrationsStep 2: Effect of running
If no migration files exist,migratewithout new migration filesmigratefinds nothing to apply, so the database stays unchanged.Final Answer:
Nothing changes in the database schema -> Option CQuick Check:
No migration files = migrate does nothing = B [OK]
- Expecting migrate to detect model changes automatically
- Assuming migrate creates migration files
- Thinking database updates without migration files
Ready to prove it?
Pass the test and this pattern moves unknown
