0
0
Djangoframework~20 mins

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

Choose your learning style9 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.