Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reordering Initial Migration operations

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.

like image 986
Nikolay Marinov Avatar asked Mar 21 '26 15:03

Nikolay Marinov


1 Answers

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")]
like image 161
Oleg Russkin Avatar answered Mar 23 '26 04:03

Oleg Russkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!