QuickDevGuide

Home › Django

Migrations — Django

Migrations are version-controlled Python files describing schema changes. Django compares your models with the latest migration state to generate new ones. Command Purpose makemigrations [app] generate migration files from model changes migrate [app] [number] apply (or roll back to) migrations showmigrations list applied / pending sqlmigrate app 0002 print the SQL a migration will run makemigrations --empty app blank migration for data changes migrate app zero unapply all of an app's migrations Data migrations Use migrations.RunPython(forward, backward) to transform existing rows. Inside it, use apps.getmodel("app", "Model") — the historical model — not a direct import. Safe changes on large tables Add nullable columns first, backfill, then add constraints. Never edit a migration that…

Key Takeaways