0
0
Djangoframework~20 mins

Database migration in production in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you run python manage.py migrate in production?
You run python manage.py migrate on a live Django app. What is the immediate effect on the database?
AIt rolls back the last migration applied to the database.
BIt deletes all data in the database and recreates tables from scratch.
CIt only checks for migration files but does not change the database.
DIt applies all unapplied migrations in order, updating the database schema accordingly.
Attempts:
2 left
💡 Hint
Think about what migrations are designed to do in Django.
📝 Syntax
intermediate
1:30remaining
Which command safely creates a new migration after model changes?
You changed a Django model in your app. Which command generates the migration file correctly?
Apython manage.py makemigrations
Bpython manage.py migrate
Cpython manage.py migrate --create
Dpython manage.py makemigration
Attempts:
2 left
💡 Hint
One command creates migration files, the other applies them.
🔧 Debug
advanced
2:30remaining
Why does this migration fail in production?
You added a non-nullable field without a default to a model and ran makemigrations. Applying the migration in production fails. Why?
Django
class MyModel(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()  # new field, no default, not nullable
ABecause the migration file is missing a <code>RunPython</code> operation.
BBecause existing rows have no value for the new non-nullable field, causing a NOT NULL constraint error.
CBecause Django migrations do not support adding new fields.
DBecause the database user lacks permission to alter tables.
Attempts:
2 left
💡 Hint
Think about what happens when you add a required column to a table with existing data.
🧠 Conceptual
advanced
3:00remaining
What is the safest way to deploy a schema migration on a live Django app with heavy traffic?
You must add a new column to a large table without downtime. Which approach is safest?
AAdd the new column as nullable first, deploy code using it, then backfill data and make it non-nullable in a later migration.
BAdd the new column as non-nullable with no default and deploy immediately.
CSkip migrations and manually update the database schema after deployment.
DDrop the table and recreate it with the new schema during deployment.
Attempts:
2 left
💡 Hint
Think about how to avoid locking or errors on large tables.
state_output
expert
3:00remaining
What is the state of the Django migration table after running python manage.py migrate --fake appname 0003?
You run migrate --fake to mark migration 0003 as applied without running it. What does Django record in the migration history?
AMigration 0003 is applied normally, running all schema changes.
BMigration 0003 is skipped and not recorded in the migration history.
CMigration 0003 is marked as applied in the database, but its schema changes are not executed.
DMigration 0003 is rolled back if previously applied.
Attempts:
2 left
💡 Hint
The --fake option marks migrations as done without running them.