Django Migrations#

Migrating in django is sometimes tricky and a bit less intuitive if youhave come fromsomething like yii or laravel

The workflow#

  1. Create the model class in

  2. Make sure the app is added to INSTALLED_APPS

  3. Create the migration

    ./ makemigrations

  4. Migrate

    ./ migrate


RunPython is a way to initialise data or run operations during migrations. They are tightly coupled with migrations and can be viewed here: RunPython docs

Reversing all migrations to Zero#

./ migrate <app_name> zero

Unapplying migrations#

Say for example you are playing around with the model and make multiple changes and multiple migrations before deciding on the correct way.

You will probably see something like this in git:

    Untracked files:
    (use "git add <file>..." to include in what will be committed)


Now you want to remove these testing migrations and just apply a single one.

It can be done by unapplying migrations to a specific version with:

./ migrate <app_name> <migration_name>
./ migrate entries 0005_auto_20171103_1341

You can then delete the migrations and create a single one afresh with:

./ makemigrations
./ migrate