3

Every time I try to migrate my initial migration, right after makemigrations, I get errors like :

django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'Project.Class'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)

The reason I think this happens is because the order of model operations in the 0001_initial.py migration is incorrect. Operations with classes which inherit from others are added before their parents'. After I reorder the operations, it works: Process finished with exit code 0. Cool! But how do I make makemigrations work without doing this every time?

Thanks!

ps. I tried reordering the import order of my models in the model's __init__.py but it didn't work.

Nikolay Marinov
  • 2,381
  • 2
  • 7
  • 12
  • Take a look if you are inheriting from a class in your model from an app that's not added to INSTALLED_APPS – Yasiel Cabrera Dec 06 '19 at 23:36
  • The classes I get the error for are in the same app. :/ – Nikolay Marinov Dec 06 '19 at 23:39
  • Ah, actually no - I inherit from other apps but I've checked and they're in INSTALLED_APPS – Nikolay Marinov Dec 06 '19 at 23:41
  • Do you have some custom migration maybe? If you are using a control version try deleting all the migrations and create them again, this is a hardcoded way to see if your old migrations have errors. It's a little difficult to tell you what could be wrong without to be seeing the project :( – Yasiel Cabrera Dec 06 '19 at 23:52
  • I'm not aware of having any customisation. It's just the auto-generated initial migration. And yes, I delete all migrations before retrying this process. :/ Thanks anyway :)) – Nikolay Marinov Dec 07 '19 at 00:10
  • 1
    Can you update with your models please. – Lord Elrond Jan 11 '20 at 04:12
  • 1
    Could you provide a minimal working example? Or the complete code base? Otherwise it is hard to help you. – Kound Jan 13 '20 at 10:42

1 Answers1

2

If you have several apps in your Django project and in models of one app models of another app are referenced - this may result in such conflict.

It is recommended to create migrations for each app separately and reference another app migration as a dependency in referencing migration file.

python manage.py makemigrations app-one
python manage.py makemigrations app-two

# example of referencing dependent migration,
# so app-two 0001 migrations runs after app-one 0001 migration
# app-two/migrations/0001-initial.py

dependencies = [("app-one", "0001-init.py")]
Oleg Russkin
  • 4,234
  • 1
  • 8
  • 20