Binsoi
Binsoi

Reputation: 383

Django makemigrations: ValueError: Lookup failed for model referenced by field

i got this error.

  Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 125, in handle
    migration_name=self.migration_name,
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 110, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 158, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 236, in __init__
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model)) ValueError: Lookup failed for model referenced by field systech_account.User.companies: systech_account.Company

every time i try to

python manage.py makemigrations

in my (Ubuntu). but when i try to run it on my Windows and my colleagues in their(Ubuntu) it works fine. (*we share the project via Git repo). they can make the migrations successfully. we are wondering why is this happening to my PC alone.

Notes:

Solutions we tried:


  1. How do we solve this?

Thanks! :)

Upvotes: 2

Views: 643

Answers (1)

Sayse
Sayse

Reputation: 43300

I'm going to take a guess here that systech_account.Company is a model thats in an app that hasn't been loaded yet whilst trying to create a migration for a different app.

If this is true its probably solvable by first running

makemigrations app_that_has_company_in_it

before running your other makemigrations.

But this isn't the solution.

The fact that you and your colleagues are all trying to do the same migration tells me that you don't store the migrations in your source control which is the real problem here.

Doing this does stop these errors from ever occurring (at least for me/us) and makes it much quicker when trying to make migrations since there aren't any repeated steps. If you're worried about merge conflicts with these, you needn't worry, django is very clever.

Upvotes: 2

Related Questions