[Fixed]-Django: ValueError: Lookup failed for model referenced by field account.UserProfile.user: auth.User

6👍

This is already fixed in the master branch.

Fixed in commits:

You can install it until a proper release is built:

pip install https://github.com/django/django/zipball/master

Test:

models.py

from django.db import models
from django.contrib.auth.models import User

class Test(models.Model):
  user = models.OneToOneField(User)

Results

[__env] $ ./manage.py makemigrations
  Migrations for 'data':
  0001_initial.py:
    - Create model Test
[__env] $ ./manage.py migrate
  Operations to perform:
  Synchronize unmigrated apps: admin, contenttypes, auth, sessions
    (... ommited ...)
  Running migrations:
  Applying data.0001_initial... OK

3👍

I read the comment above as suggesting adding only the last line in the dependencies list (('contenttypes',' __first__')), but I had to add the last two to correct the problem. This might be obvious to those with a better understanding of the makemigrations tool in Django, but I am new to it and it was not to me.

My last migrations file dependencies list that was originally giving the same error…

dependencies = [
    ('myapp', '0006_auto_20150209_0324'),
    (b'auth', b'__first__'),
    (b'contenttypes', b'__first__'),
]

2👍

I’ve found another solution that also work fine.

See the solution of by rockallite.wulf@
https://code.djangoproject.com/ticket/22488

Just add the corresponding dependency in the migration file, like this:

dependencies = [
        (b'advperm', b'0001_initial'),
        (b'auth', b'__first__'),
        (b'contenttypes', b'__first__'),  # Add this line
]
👤aul

Leave a comment