Migrations are version-controlled schema changes—like Git for your database structure. Django generates them from model changes and applies them with migrate.
Workflow
- Edit
models.py python manage.py makemigrations— creates migration filespython manage.py migrate— applies to database
Best practices
- Commit migration files to git—teams must share the same history
- Review auto-generated migrations before deploying
- Never edit applied migrations in production—create new ones
- Data migrations use
RunPythonfor backfills
Important interview questions and answers
- Q: makemigrations vs migrate?
A: makemigrations writes Python migration files from model diffs; migrate executes them against the DB. - Q: Squashing migrations?
A: Combines many migrations into one for cleaner history in long projects—plan carefully. - Q: Zero-downtime deploys?
A: Add nullable columns first, backfill, then enforce NOT NULL in a follow-up migration.
Self-check
- Which command creates migration files?
- Why commit migrations to version control?
Tip: Run makemigrations after every model change and commit the files—never "fix" production by hand-editing tables without a migration.
Interview prep
- makemigrations vs migrate?
makemigrations writes migration files from model changes; migrate applies them to the database schema.