[Fixed]-Django: IntegrityError: column user_id is not unique

6👍

I worked around the problem by adjusting my signal like this:

from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.get_or_create(user=instance)

This solved the symptom but not really the cause. I think that mixing normal tests with Django tests is causing an error somewhere. When I ran the test in my question alone it would work.

If I get no other answers I’ll mark this one as correct.

5👍

I ran into the same problem and there’s a simple fix. The problem happens if you run ‘manage.py dumpdata’ and you already have a UserProfile in your database. The UserProfile will be in the json file, so when you load the test data fixture and try to create a new UserProfile with the same username in your test, you will get a conflict since a UserProfile with that User already exists. The solution is to just delete the UserProfile from the json fixture.

So, as an example:

  • You have an existing database with User with the username ‘mathew’ and a UserProfile tied that User
  • You run manage.py dumpdata
  • Now the resulting json fixture has the UserProfile in it, because it is part of your app’s models (but not the User)
  • Now you try to create a user in your test like this: ‘User.objects.create_user(‘mathew’, ‘mathew@example.com’, ‘password’)’
  • Your post save signal will trigger and try to create a UserProfile associated to User ‘mathew’
  • But that UserProfile already exists and so you get an error

Hope that helps.

👤mathew

1👍

I suppose you have got user_id field with unique constraint in Profile model, haven’t you?

It seems that you’re trying to save Profile related to the same User object in some other place in your code. get_or_create shortcut works fine, because it creates new object only if there is no such object in database. Otherwise it returns existing object. On the other hand, Profile().save() just tries to save object and raises an exception if it isn’t possible.

Does it make sense for you?

Leave a comment