Alasdair's answer gives you a disclaimer, but reverting to a previous migration is only safe if your migration is idempotent, which means you can run it several times without side effects such as duplicate data. Most people do not write their migrations this way, but this is good practice.
You have two ways to make this process safe:
- Make your data migrations idempotent. This means that any data created is either reused (for example, using the
Model.objects.get_or_create() method), or deleted and recreated. Reuse is the best option since deleting and re-creating will change indexes and database sequences. - Make backward data migration. You can do this by passing 2 functions in
migrations.RunPython() . For example, if you have migrations.RunPython(add_countries) , you should change this to migrations.RunPython(add_countries, remove_countries) and remove any relevant countries in the second function.
If you choose option number 2, you run:
./manage.py migrate yourapp 0010_my_previous_data_migration ./manage.py migrate yourapp 0011_my_data_migration
If you want to make one liner so that it can be used again and again:
./manage.py migrate
source share