[Django]-ValueError: Related model u'mutech.branch' cannot be resolved

8πŸ‘

βœ…

The solution which worked for me is to delete my migrations folder and database completely thereafter running following commands-

python manage.py makemigrations

python manage.py migrate

because this error occured to me due to some misplacement of foreign key, and I was getting the error even after undoing the changes.

We are deleting the migration folder in the app because the actual problem is with that folder and there is nothing special in migration folder and it will be recreated using your model.py file running the command –python manage.py makemigrations. The solution is just to delete the Migration folder and recreate it using commands.

So what you have to do-

  1. Delete migration folder from the app.
  2. Run the commands python manage.py makemigrations and then python manage.py migrate

Caution: The data in the database will be lost after this, So perform this only if your data is not important.

πŸ‘€Rahul Satal

2πŸ‘

This is how I solved this problem:

  1. Create a new ForeignKey call tmp to your related model. Run a migration.
  2. Delete the old ForeignKey and run a migration.
  3. Rename tmp to whatever the old ForeignKey was called. Run a migration.

So, you end up having three migration files to do one thing but at least it does it!

2πŸ‘

This problem is caused by cyclic dependency in your migrations. Some other migration that is run prior to the latest migration is calling a migration that reinstates the state before model rename. For example, you renamed model foo to bar in xyzzy.migrations.0004_rename but during applying migrations, after xyzzy.migrations.0004_rename runs bozotic.0002_bozo that depends on xyzzy.migrations.0001_initial thus the migration that has to be applied now doesn’t see the state created by xyzzy.migrations.004_rename.

Took me an hour to debug and fix this error in my project.

Check your migration dependencies and try manually adjusting dependencies so that when running the migration models are in the desired state

Leave a comment