Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model u'id' clashes when using OneToOneField

I have created a model UserProfile to relate model User.

I got error when doing python manage.py makemigrations:

django.core.exceptions.FieldError: Local field u'id' in class 'UserProfile' clashes with field of similar name from base class 'User'

Here's the code:

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

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user_of')
    description = models.TextField()

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)

Environment: Django 1.8.2, Python 2.7.6, PostgreSQL 9.4.2

Is that a bug? How come does the u'id' of 'UserProfile' clashes with that of 'User'...

I have tried to add a line in the file settings.py:

AUTH_PROFILE_MODULE = 'user_profile.UserProfile'

But that did not work whether it has or not.

How to fix this? Thanks!

like image 824
dotslash Avatar asked May 26 '15 18:05

dotslash


1 Answers

What you probably have is an old migration that used to inherit from the model in question

To test this out clone your project and delete all the migrations and makemigrations on a fresh new database

If it works, then track down the offending migration from your current project and don't forget to remove the entry from django_migrations table as well

like image 88
Dr Manhattan Avatar answered Sep 19 '22 04:09

Dr Manhattan