This is an extension to this question: How to move a model between two Django apps (Django 1.7)
I need to move a bunch of models from old_app
to new_app
. The best answer seems to be Ozan's, but with required foreign key references, things are bit trickier. @halfnibble presents a solution in the comments to Ozan's answer, but I'm still having trouble with the precise order of steps (e.g. when do I copy the models over to new_app
, when do I delete the models from old_app
, which migrations will sit in old_app.migrations
vs. new_app.migrations
, etc.)
Any help is much appreciated!
After work was done I tried to make new migration. But I facing with following error:
ValueError: Unhandled pending operations for models: oldapp.modelname (referred to by fields: oldapp.HistoricalProductModelName.model_ref_obj)
If your Django model using
HistoricalRecords
field don't forget add additinal models/tables while following @Nostalg.io answer.Add following item to
database_operations
at the first step (4.a):and add additional Delete into
state_operations
at the last step (4.d):In case you need to move the model and you don't have access to the app anymore (or you don't want the access), you can create a new Operation and consider to create a new model only if the migrated model does not exist.
In this example I am passing 'MyModel' from old_app to myapp.
I've built a management command to do just that - move a model from one Django app to another - based on nostalgic.io's suggestions at https://stackoverflow.com/a/30613732/1639699
You can find it on GitHub at alexei/django-move-model
Migrating a model between apps.
The short answer is, don't do it!!
But that answer rarely works in the real world of living projects and production databases. Therefore, I have created a sample GitHub repo to demonstrate this rather complicated process.
I am using MySQL. (No, those aren't my real credentials).
The Problem
The example I'm using is a factory project with a cars app that initially has a
Car
model and aTires
model.The
Car
model has a ForeignKey relationship withTires
. (As in, you specify the tires via the car model).However, we soon realize that
Tires
is going to be a large model with its own views, etc., and therefore we want it in its own app. The desired structure is therefore:And we need to keep the ForeignKey relationship between
Car
andTires
because too much depends on preserving the data.The Solution
Step 1. Setup initial app with bad design.
Browse through the code of step 1.
Step 2. Create an admin interface and add a bunch of data containing ForeignKey relationships.
View step 2.
Step 3. Decide to move the
Tires
model to its own app. Meticulously cut and paste code into the new tires app. Make sure you update theCar
model to point to the newtires.Tires
model.Then run
./manage.py makemigrations
and backup the database somewhere (just in case this fails horribly).Finally, run
./manage.py migrate
and see the error message of doom,django.db.utils.IntegrityError: (1217, 'Cannot delete or update a parent row: a foreign key constraint fails')
View code and migrations so far in step 3.
Step 4. The tricky part. The auto-generated migration fails to see that you've merely copied a model to a different app. So, we have to do some things to remedy this.
You can follow along and view the final migrations with comments in step 4. I did test this to verify it works.
First, we are going to work on
cars
. You have to make a new, empty migration. This migration actually needs to run before the most recently created migration (the one that failed to execute). Therefore, I renumbered the migration I created and changed the dependencies to run my custom migration first and then the last auto-generated migration for thecars
app.You can create an empty migration with:
Step 4.a. Make custom old_app migration.
In this first custom migration, I'm only going to perform a "database_operations" migration. Django gives you the option to split "state" and "database" operations. You can see how this is done by viewing the code here.
My goal in this first step is to rename the database tables from
oldapp_model
tonewapp_model
without messing with Django's state. You have to figure out what Django would have named your database table based on the app name and model name.Now you are ready to modify the initial
tires
migration.Step 4.b. Modify new_app initial migration
The operations are fine, but we only want to modify the "state" and not the database. Why? Because we are keeping the database tables from the
cars
app. Also, you need to make sure that the previously made custom migration is a dependency of this migration. See the tires migration file.So, now we have renamed
cars.Tires
totires.Tires
in the database, and changed the Django state to recognize thetires.Tires
table.Step 4.c. Modify old_app last auto-generated migration.
Going back to cars, we need to modify that last auto-generated migration. It should require our first custom cars migration, and the initial tires migration (that we just modified).
Here we should leave the
AlterField
operations because theCar
model is pointing to a different model (even though it has the same data). However, we need to remove the lines of migration concerningDeleteModel
because thecars.Tires
model no longer exists. It has fully converted intotires.Tires
. View this migration.Step 4.d. Clean up stale model in old_app.
Last but not least, you need to make a final custom migration in the cars app. Here, we will do a "state" operation only to delete the
cars.Tires
model. It is state-only because the database table forcars.Tires
has already been renamed. This last migration cleans up the remaining Django state.Nostalg.io's way worked in forwards (auto-generating all other apps FKs referencing it). But i needed also backwards. For this, the backward AlterTable has to happen before any FKs are backwarded (in original it would happen after that). So for this, i split the AlterTable in to 2 separate AlterTableF and AlterTableR, each working only in one direction, then using forward one instead of the original in first custom migration, and reverse one in the last cars migration (both happen in cars app). Something like this:
Just now moved two models from
old_app
tonew_app
, but the FK references were in some models fromapp_x
andapp_y
, instead of models fromold_app
.In this case, follow the steps provided by Nostalg.io like this:
old_app
tonew_app
, then update theimport
statements across the code base.makemigrations
.AlterModelTable
for all moved models. Two for me.state_operations
instead.DeleteModel
for all moved models.Notes:
old_app
whereAlterModelTable
is used to rename the table(s). (created in Step 4.a.)old_app
because I didn't have anyAlterField
operations, onlyDeleteModel
andRemoveField
operations. Or keep it with emptyoperations = []
To avoid migration exceptions when creating the test DB from scratch, make sure the custom migration from
old_app
created at Step 4.a. has all previous migration dependencies from other apps.BTW: There is an open ticket about this: https://code.djangoproject.com/ticket/24686