Bird
Raised Fist0
Djangoframework~20 mins

Migrations concept and workflow in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Migration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens after running python manage.py migrate?
You have created a new model in your Django app and run python manage.py makemigrations. What is the immediate effect of running python manage.py migrate?
AIt restarts the Django development server automatically.
BIt deletes all existing data in the database tables.
CIt only creates migration files but does not change the database.
DIt applies the new migration files to update the database schema accordingly.
Attempts:
2 left
💡 Hint
Think about what changes the database structure.
📝 Syntax
intermediate
1:30remaining
Identify the correct migration command to create migration files
Which command correctly creates migration files after you change your Django models?
Apython manage.py makemigrations
Bpython manage.py migrate
Cpython manage.py runserver
Dpython manage.py collectstatic
Attempts:
2 left
💡 Hint
This command prepares the changes but does not apply them yet.
🔧 Debug
advanced
2:30remaining
Why does this migration fail with an error?
You added a new non-nullable field without a default to an existing model and ran makemigrations. The migration fails when applying. Why?
Django
class Book(models.Model):
    title = models.CharField(max_length=100)
    published_date = models.DateField()
    author = models.CharField(max_length=100, null=False)  # New field added without default
ABecause the <code>author</code> field should be a ForeignKey, not CharField.
BBecause the migration files were not created with <code>makemigrations</code>.
CBecause the new field is non-nullable and existing rows have no value, so the database rejects the migration.
DBecause Django does not support adding new fields to models.
Attempts:
2 left
💡 Hint
Think about what happens to existing data when adding a required field.
🧠 Conceptual
advanced
2:00remaining
What is the role of the Migration class in Django migration files?
Each migration file contains a Migration class. What does this class define?
AIt defines the operations to apply or reverse changes to the database schema.
BIt contains the Django model definitions for the app.
CIt manages user authentication during migrations.
DIt stores the database connection settings.
Attempts:
2 left
💡 Hint
Think about what migrations do to the database.
❓ state_output
expert
3:00remaining
What is the state of the database after running these commands in order?
You run these commands in your Django project:

1. python manage.py makemigrations
2. python manage.py migrate
3. You modify a model by renaming a field
4. python manage.py makemigrations
5. python manage.py migrate

What is true about the database after step 5?
AThe database schema is reset to the initial state before any migrations.
BThe database schema matches the latest model changes, including the renamed field.
CThe database schema has duplicate columns for old and new field names.
DThe database schema still has the old field name because migrations do not rename fields.
Attempts:
2 left
💡 Hint
Consider what migrations do when you rename a field and apply migrations.

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