Migrations Questions with Answers

13 practice questions on Migrations (Django) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

Which command lists every migration and marks which ones are applied?

  1. python manage.py listmigrate
  2. python manage.py migrations --status
  3. python manage.py showmigrations
  4. python manage.py statusdb

Answer: C. python manage.py showmigrations

Explanation

Applied migrations show [X]; pending ones show [ ].

Step-by-step solution

  1. Run showmigrations.
  2. Read the checkboxes.

Why the other options are wrong

  • A. python manage.py listmigrate is not a Django management command; the correct choice is python manage.py showmigrations.
  • B. python manage.py migrations --status is not a valid flag form; the correct choice is python manage.py showmigrations.
  • D. python manage.py statusdb is not a Django management command; the correct choice is python manage.py showmigrations.

Question 2Easy

Which command applies pending migrations to the database?

  1. python manage.py makemigrations
  2. python manage.py migrate
  3. python manage.py applydb
  4. python manage.py upgrade

Answer: B. python manage.py migrate

Explanation

migrate runs unapplied migrations.

Step-by-step solution

  1. Python manage.py migrate applies pending migrations.
  2. This command updates the database schema.
  3. It executes all unrun migration files.
  4. The correct command is python manage.py migrate.
  5. Final answer: B (python manage.py migrate).

Why the other options are wrong

  • A. makemigrations creates migration files but does not apply them to the database.
  • C. applydb is not a Django command; migrate is the command that applies migrations.
  • D. upgrade is not a Django command; migrate applies pending migrations.

Tip: Remember: migrate applies changes, makemigrations creates them.

Question 3Easy

Which command generates migration files from model changes?

  1. python manage.py migrate
  2. python manage.py syncdb
  3. python manage.py makemigrations
  4. python manage.py updatemodels

Answer: C. python manage.py makemigrations

Explanation

makemigrations writes files; migrate applies them.

Step-by-step solution

  1. Makemigrations creates migration files from model changes.
  2. Migrate applies the migrations to the database.
  3. Syncdb is for older Django versions and not used now.
  4. Updatemodels is not a valid command in Django.
  5. Final answer: python manage.py makemigrations.

Why the other options are wrong

  • A. migrate applies migrations to the database; it does not generate them from model changes.
  • B. syncdb is an old command replaced by migrations; it does not generate migration files.
  • D. updatemodels is not a Django command; migration files come from makemigrations.

Tip: Remember: makemigrations creates files, migrate applies them.

Question 4Easy

How do you roll back app blog to migration 0002?

  1. python manage.py migrate blog 0002
  2. python manage.py rollback blog 0002
  3. python manage.py unmigrate
  4. python manage.py migrate --undo

Answer: A. python manage.py migrate blog 0002

Explanation

Migrating to an earlier name reverses later migrations.

Step-by-step solution

  1. Name app.
  2. Give target.

Why the other options are wrong

  • B. No rollback command.
  • C. python manage.py unmigrate is not a Django management command; the correct choice is python manage.py migrate blog 0002.
  • D. migrate has no --undo flag; you roll back by naming the earlier migration.

Question 5Medium

What does the dependencies list inside a migration file declare?

  1. Python packages to install first
  2. The database tables to create
  3. The apps allowed to import the model
  4. Other migrations that must be applied before this one

Answer: D. Other migrations that must be applied before this one

Explanation

Django uses dependencies to build the order in which migrations run.

Step-by-step solution

  1. Read dependencies.
  2. Order is resolved.

Why the other options are wrong

  • A. Packages belong to requirements files.
  • B. Operations list the changes.
  • C. Imports are not governed there.

Question 6Medium

Which command shows the SQL a migration would run without executing it?

  1. python manage.py sqlmigrate app 0001
  2. python manage.py showsql app 0001
  3. python manage.py dbshell app 0001
  4. python manage.py inspectdb app 0001

Answer: A. python manage.py sqlmigrate app 0001

Explanation

sqlmigrate prints the SQL.

Step-by-step solution

  1. Sqlmigrate command generates SQL for a migration.
  2. It shows the SQL without running it.
  3. Python manage.py sqlmigrate app 0001 is the correct command.
  4. This matches option A.
  5. Final answer: python manage.py sqlmigrate app 0001.

Why the other options are wrong

  • B. showsql is not a Django command; sqlmigrate prints the SQL for a migration.
  • C. dbshell opens a database shell; it does not print the SQL of a migration.
  • D. inspectdb builds models from an existing database; it does not show migration SQL.

Tip: Use sqlmigrate to preview migration SQL before execution.

Question 7Medium

What does makemigrations do?

  1. Applies the SQL to the database and records each file in the migrations table
  2. Deletes tables
  3. Rolls back
  4. Creates migration files from model changes without touching the database

Answer: D. Creates migration files from model changes without touching the database

Explanation

It compares model state with existing migrations.

Step-by-step solution

  1. Change model.
  2. Run makemigrations.

Why the other options are wrong

  • A. That is migrate.
  • B. Not destructive.
  • C. migrate with a target does that.

Question 8Medium

Where does Django record which migrations have been applied?

  1. In a migrations.lock file at the project root
  2. In the settings.py module of the project
  3. In the __init__.py file of each app package
  4. In the django_migrations table

Answer: D. In the django_migrations table

Explanation

The django_migrations table stores applied names.

Step-by-step solution

  1. Django tracks applied migrations in a database table.
  2. The table is named django_migrations by default.
  3. Each migration is recorded with its name and app.
  4. This ensures no duplicate migrations are run.
  5. Final answer: In the django_migrations table.

Why the other options are wrong

  • A. There is no migrations.lock file; Django records applied migrations in the database.
  • B. settings.py holds configuration, not the migration history.
  • C. __init__.py only marks a package; it does not store which migrations were applied.

Tip: Remember, Django stores migration status in the database, not files.

Question 9Hard

You add a non-nullable field with no default to a model with existing rows. What does makemigrations do?

  1. Silently fills every existing row with NULL values
  2. Drops the existing rows before adding the column
  3. Fails and creates no migration file at all
  4. Prompts for a one-off default, or lets you cancel and set one

Answer: D. Prompts for a one-off default, or lets you cancel and set one

Explanation

Django prompts because existing rows need a value.

Step-by-step solution

  1. Django needs a value for the new field in existing rows.
  2. It asks for a one-off default or lets you cancel.
  3. No migration is created without this input.
  4. The user must provide a default or choose to set one.
  5. Final answer: Prompts for a one-off default, or lets you cancel and set one.

Why the other options are wrong

  • A. Django does not fill rows with NULL; a NOT NULL column cannot hold NULL, so that would fail.
  • B. Existing rows are kept; Django never drops data to add a column.
  • C. makemigrations does create a migration file once a default is given or you choose to set one.

Tip: Non-nullable fields need defaults; Django asks for them.

Question 10Hard

Which command shows the SQL a migration would run?

  1. python manage.py showsql blog
  2. python manage.py sqlmigrate blog 0001
  3. python manage.py dbshell blog
  4. python manage.py inspectdb blog

Answer: B. python manage.py sqlmigrate blog 0001

Explanation

sqlmigrate prints the SQL without executing it.

Step-by-step solution

  1. Give app and number.
  2. SQL printed.

Why the other options are wrong

  • A. python manage.py showsql blog is not a Django management command; the correct choice is python manage.py sqlmigrate blog 0001.
  • C. dbshell opens a client.
  • D. inspectdb reads an existing DB.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Django topics · All topics