Django migrations error KeyError: ('list', u'user') Django migrations error KeyError: ('list', u'user') python python

Django migrations error KeyError: ('list', u'user')


I ran into a similar issue, where db\migrations\operations\models.py was throwing a KeyError after renaming a model through PyCharm's refactoring (renaming).

Apparently the refactoring also took place in the migration file. When opening up the migration file and changing back to the original naming, the makemigrations command worked fine.


The problem was in migration files.While I was making commit into git somehow I've deleted one of the migration files, so the order was like 0001 0003 0004 without 0002.In the second migration file I've created a model named user.

The problem was that when I run python manage.py migrate django could not find the place where the model named user has been created (this model has been created in 0002 file).

I solved it by manually adding this code to the 0001 migration file:

migrations.CreateModel(        name='user',        fields=[            (...necessary fields...),        ],        options={            'ordering': ('title',),        },    ),


I had the same issue and found that the easiest solution, if you models.py is intact, was just to delete all the old migrate files and then run makemigrations again. I don't think squashmigrations would help, since it only brings together all the different migration files into one, and it migrates on the basis of current migrate files. Which doesn't help if your migrate files are somehow corrupted. Which is what causes this issue in the first place.