Using makemigrations and migrate Commands in Django
📖 Scenario: You are building a simple Django app to manage a library's book collection. You need to create the database tables for your app models and apply changes to the database using Django's migration system.
🎯 Goal: Learn how to create migration files with makemigrations and apply them to the database with migrate commands in Django.
📋 What You'll Learn
Create a Django model for books
Generate migration files using makemigrations
Apply migrations to the database using migrate
Verify the migration commands are correctly used
💡 Why This Matters
🌍 Real World
Django developers use migrations to safely and easily update database schemas as their app models change over time.
💼 Career
Understanding migrations is essential for backend developers working with Django to manage database changes without data loss.
Progress0 / 4 steps
1
Create a Django model for books
In your Django app's models.py file, create a model class called Book with these exact fields: title as a CharField with max length 100, and author as a CharField with max length 50.
Django
Hint
Use models.CharField for text fields and set max_length exactly as specified.
2
Create migration files with makemigrations
Run the Django management command python manage.py makemigrations in your terminal to create migration files for the Book model you just defined.
Django
Hint
This command creates migration files that describe the changes in your models.
3
Apply migrations to the database with migrate
Run the Django management command python manage.py migrate in your terminal to apply the migration files and create the database tables for the Book model.
Django
Hint
This command applies the migrations and creates the tables in your database.
4
Verify migration commands usage
Add comments in your code showing the exact two commands you used: python manage.py makemigrations and python manage.py migrate to complete the migration process.
Django
Hint
Write the two commands as comments exactly as shown.
Practice
(1/5)
1. What is the main purpose of the makemigrations command in Django?
easy
A. To delete old migration files
B. To apply changes to the database
C. To start the Django development server
D. To create migration files that describe changes in models
Solution
Step 1: Understand the role of makemigrations
This command generates files that describe changes made to Django models, like adding or removing fields.
Step 2: Differentiate from migrate
migrate applies those changes to the database, but makemigrations only creates the description files.
Final Answer:
To create migration files that describe changes in models -> Option D
Quick Check:
makemigrations creates files = A [OK]
Hint: Remember: makemigrations creates files, migrate applies them [OK]
Common Mistakes:
Confusing makemigrations with migrate
Thinking makemigrations changes the database directly
Assuming makemigrations starts the server
2. Which of the following is the correct command to apply migrations to the database in Django?
easy
A. python manage.py makemigrations
B. python manage.py migrate
C. python manage.py runserver
D. python manage.py makemigrate
Solution
Step 1: Identify the command to apply migrations
The command python manage.py migrate applies migration files to update the database schema.
Step 2: Check other options
makemigrations creates migration files, runserver starts the server, and makemigrate is not a valid command.
Final Answer:
python manage.py migrate -> Option B
Quick Check:
Apply migrations = migrate = A [OK]
Hint: Use migrate to apply changes, not makemigrations [OK]
Common Mistakes:
Typing makemigrate instead of migrate
Using makemigrations to update the database
Confusing runserver with migration commands
3. Given the following sequence of commands in a Django project: