[Solved]-Is it safe to do a data migration as just one operation in a larger Django migration?

11👍

With regards to Django itself, this is perfectly safe. Each operation will receive the correct state based on all previous migrations and operations within the same migration. Your RunPython operation will receive an app registry that includes the new Bar model and still has the old field on Foo.

What may not be safe is the database-side of the operation. If a database supports DDL (Data Definition Language) in transactions, Django will run the complete migration in a single transaction. PostgreSQL, for example, supports DDL in transactions, but does not allow you to mix schema changes and data changes in the same transaction. Attempting to do both within a single migration/transaction will result in an error.

If you use MySQL or Oracle, which do not support DDL transactions and will only run the RunPython operation in a transaction, you can safely put all operations in the same migration. However, you will lose out on some cross-database compatibility.

👤knbk

Leave a comment