Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to run

python manage.py migrate

or

python manage.py makemigrations

I got this error:

Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 183, in handle
    executor.loader.project_state(),
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/loader.py", line 338, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/graph.py", line 280, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/migration.py", line 88, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 547, in state_forwards
    model_state = state.models[app_label, self.name_lower]
KeyError: ('list', u'user')

It happen after I pulled another version of my app from the git.

I don't have this error with the same code on the another machine. I've tried to use --fake with zero or to squashmigrations to previous but this also doesn't help.

Cannot get how to solve it.

like image 686
Roberto Avatar asked Jan 19 '16 17:01

Roberto


3 Answers

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.

like image 129
SaeX Avatar answered Oct 27 '22 11:10

SaeX


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',),
        },
    ),
like image 44
Roberto Avatar answered Oct 27 '22 11:10

Roberto


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.

like image 30
Rijo Simon Avatar answered Oct 27 '22 10:10

Rijo Simon