pythondjangodatabase-migration

Renaming model breakes migration history


I have two models in two different apps:

# app1 models.py
class App1Model(models.Model):
    pass


# app2 models.py
from app1.models import App1Model

class App2Model(App1Model):
    pass

And I want to rename App1Model and then recreate App2Model to have explicit OneToOneField instead of magic app1model_ptr. So I create migration which deletes App2Model completely (I don't care about data because whatever reason), it runs successfully. Then I create migration in first app which renames App1Model and it runs perfect too, I check this table with new name and all it's relationships (and to it too), it's fine.

And then weird thing happens: when I run makemigrations or migrate on app2 I get an error

django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'app2.App2Model'>]

It fails while creating current project state on very first migration of app2 (0001_initial.py in app2 migrations) where this model was created first time by inheriting from App1Model with its old name. Is there some way of fixing this? In current state App2Model already deleted, App1Model renamed and I can't do anything with migrations on app2 because of this issue.

P.S. I use Django 1.10.2


Solution

  • Just found solution:

    Need to add last migration of app2 where I deleted App2Model as dependency to migration of app1 where I renamed App1Model so project state will be built in correct order. Actually error message itself has something related to it but I failed to catch the point:

    This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth) in an app with no migrations; see https://docs.djangoproject.com/en/1.10/topics/migrations/#dependencies for more

    I put it here for future me and for those who will suffer from similar thing.