[Django]-Get_or_create throws Integrity Error

35👍

The parameters sent into the get_or_create method need to match exactly, or django’s ORM would try to create a new object, and since a primary key/unique column constraint would be violated, you are getting the error.

Try this:

if form.is_valid():
    first_name = form.cleaned_data['first_name']
    last_name = form.cleaned_data['last_name']
    myemail = form.cleaned_data['email']
    mypass = form.cleaned_data['password']
    if myemail and mypass:
        myuser,created = User.objects.get_or_create(email=myemail, defaults = {'username': myemail, 'first_name': first_name, 'last_name': last_name})
        if created:
            myuser.set_password(mypass)

return HttpResponseRedirect('/')

Read more on get_or_create here. The defaults= argument is what you needed.

0👍

You are asking django to fetch a record based on four conditions:

  • email
  • username
  • first_name
  • last_name

So all four fields combined does not have a record.

You should do:

myuser, created = User.objects.get_or_create(
   username=myemail, defaults={'first_name': first_name, 'last_name': last_name, 'email': myemail})

0👍

I have faced similar error after redefining model’s save() method. It turns out that in case you call save more than once inside your model, all consequent calls should be made with force_insert=True keyword argument. In my case I had to change self.save(*args, **kwargs) to self.save() on second call to get rid of the error.
For more details read here: https://code.djangoproject.com/ticket/28253

Leave a comment