Rerun a Django data migration

前端 未结 2 543
南笙
南笙 2020-12-08 04:03

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.

2条回答
  •  孤街浪徒
    2020-12-08 04:54

    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:

    1. Make your data migrations idempotent. This means that any created data is either reused (like with the 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.
    2. Make reverse data migrations. You can do this by passing 2 functions to 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
    

提交回复
热议问题