How would I rerun a data migration on Django 1.8+? If relevant, my migration is numbered 0011_my_data_migration.py and is the latest migration.
Fake back to the migration before the one you want to rerun.
./manage.py migrate --fake yourapp 0010_my_previous_data_migration
Then rerun the migration.
./manage.py migrate yourapp 0011_my_data_migration
Then you can fake back to the most recent migration that you have run. In your case, you said that 0011 was the latest, so you can skip this stage.
./manage.py migrate --fake yourapp 0014_my_latest_data_migration
Note that depending on the state of your database and the contents of the migrations, rerunning a migration like this might cause errors. Note the warning in the docs about the --fake
option:
This is intended for advanced users to manipulate the current migration state directly if they’re manually applying changes; be warned that using
--fake
runs the risk of putting the migration state table into a state where manual recovery will be needed to make migrations run correctly.
Alasdair's answer gives a disclaimer about this, but faking a migration back to the previous one is only safe if your migration is idempotent, which means you can run it multiple times without side effects like duplicate data. Most people don't write their migrations this way, but it's good practice.
You have two options to make this process safe:
Model.objects.get_or_create()
method) or deleted and recreated. Reused is the better option, as deleting and recreating will change database indexes and sequences.migrations.RunPython()
. For example, if you have migrations.RunPython(add_countries)
, you would change that to migrations.RunPython(add_countries, remove_countries)
and delete any relevant countries in the second function.If you choose option #2 then you would run:
./manage.py migrate yourapp 0010_my_previous_data_migration
./manage.py migrate yourapp 0011_my_data_migration
If you wanted to make that a one liner so that you can use it over and over:
./manage.py migrate yourapp 0010_my_previous_data_migration && ./manage.py migrate yourapp 0011_my_data_migration