djangogirls icon indicating copy to clipboard operation
djangogirls copied to clipboard

IntegrityError: duplicate key value violates unique constraint "unique_form_email_email_not_null"

Open sentry-io[bot] opened this issue 1 year ago • 6 comments

Sentry Issue: DJANGO-GIRLS-WEBSITE-5H

UniqueViolation: duplicate key value violates unique constraint "unique_form_email_email_not_null"
DETAIL:  Key (form_id, email)=(790, [email protected]) already exists.

  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

IntegrityError: duplicate key value violates unique constraint "unique_form_email_email_not_null"
DETAIL:  Key (form_id, email)=(790, [email protected]) already exists.

(12 additional frame(s) were not displayed)
...
  File "applications/views.py", line 41, in apply
    form.save()
  File "applications/forms.py", line 121, in save
    application.save()
  File "applications/models.py", line 166, in save
    super().save(*args, **kwargs)

sentry-io[bot] avatar Jul 14 '23 16:07 sentry-io[bot]

Hey @anaschwendler, do you think you can pick this one up? I can help if you need my assistance. Thanks!

amakarudze avatar Jul 14 '23 16:07 amakarudze

Yup, I'll take a look on it tomorrow :)

anaschwendler avatar Jul 16 '23 14:07 anaschwendler

@amakarudze

I'm reading the error here, and I would like to know which approach I should take. Should I remove this or maybe return the form filled with the information?

Thank you!

anaschwendler avatar Jul 17 '23 11:07 anaschwendler

Hello @anaschwendler, no you should not remove that. That's just the name of the UniqueConstraint we have for the email and form to ensure that an applicant can only apply once for an event and that is set up correctly. See https://docs.djangoproject.com/en/4.2/ref/models/constraints/#uniqueconstraint.

What we could do is change the way we save the application in our view here when we save the form so that we can catch the exception and let the user know that the email address has already been used to apply for that event before using a try...except block.

The form.save() and messages.success(...) can be in the try block and then under except IntegrityError, you put messages.error(request, f"Application with email address {email} already exists for this event.").

amakarudze avatar Jul 18 '23 06:07 amakarudze

Hi! I am new to Django Girls and I want to help by contributing. Can I help out with this issue?

MariaMozgunova avatar Aug 03 '23 04:08 MariaMozgunova

Hey @MariaMozgunova, yes sure feel free to pick it up.

amakarudze avatar Aug 08 '23 12:08 amakarudze