Kaan San Kaan San - 11 months ago 173
Python Question

Django / duplicate key value violates unique constraint

I try to extend django auth user model with OneToOneField but couldn't solve this problem.


duplicate key value violates unique constraint "users_profile_user_id_key"
DETAIL: Key (user_id)=(67) already exists.


I look through this issue , some people say the db out of sync.

views.py

def create_profile(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
profile_form = UserProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
registration = user_form.save()
profile = profile_form.save(commit=False)
profile.user = registration
profile.save()
return redirect('index')
else:
user_form = UserRegistrationForm()
profile_form = UserProfileForm()

return render(request, 'registration/registration.html',
{'user_form': user_form,
'profile_form': profile_form})


models.py

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_image = models.ImageField(upload_to="blog/assets",
default="blog/assets/people-photo.jpg",
null=True)
birth_date = models.DateField(null=True, blank=True)


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

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()


forms.py

class UserRegistrationForm(forms.ModelForm):

class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password')
widgets = {
'password': forms.PasswordInput(),
}

class UserProfileForm(forms.ModelForm):

class Meta:
model = Profile
fields = ('user_image', 'birth_date',)

Answer Source

Your most trouble is: you try to create profile double time: by signal and by form. May be you can just remove the signal.

Or try to use get_or_create instead of simple create as i can see it does not break your logic

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.get_or_create(user=instance)
        #              ^^^^^^^^^^^^^^

and override save form:

if user_form.is_valid() and profile_form.is_valid():
    registration = user_form.save()
    # 
    profile_form = UserProfileForm(request.POST, instance=registration.profile)
    if profileis_valid():
        profile.save()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download