Django Migrations Working with migrations

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Django uses migrations to propagate changes you make to your models to your database. Most of the time django can generate them for you.

To create a migration, run:

$ django-admin makemigrations <app_name>

This will create a migration file in the migration submodule of app_name. The first migration will be named 0001_initial.py, the other will start with 0002_, then 0003, ...

If you omit <app_name> this will create migrations for all your INSTALLED_APPS.

To propagate migrations to your database, run:

$ django-admin migrate <app_name>

To show all your migrations, run:

$ django-admin showmigrations app_name
app_name
  [X] 0001_initial
  [X] 0002_auto_20160115_1027
  [X] 0003_somemodel
  [ ] 0004_auto_20160323_1826
  • [X] means that the migration was propagated to your database
  • [ ] means that the migration was not propagated to your database. Use django-admin migrate to propagate it

You call also revert migrations, this can be done by passing the migration name to the migrate command. Given the above list of migrations (shown by django-admin showmigrations):

$ django-admin migrate app_name 0002  # Roll back to migration 0002
$ django-admin showmigrations app_name
app_name
  [X] 0001_initial
  [X] 0002_auto_20160115_1027
  [ ] 0003_somemodel
  [ ] 0004_auto_20160323_1826


Got any Django Question?