Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntegrityError at /accounts/register/: duplicate key value violates unique constraint "front_user_username_key" #491

Open
The-Compiler opened this issue Aug 25, 2022 · 1 comment

Comments

@The-Compiler
Copy link
Member

Ich habe in letzter Zeit (24.7. bzw. 13.8.) zwei unhandled exceptions via Mail bekommen, die ich mir nicht erklären kann. Bei beiden heisst es nach einem POST auf http://studentenportal.ch/accounts/register/:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

The above exception (duplicate key value violates unique constraint "front_user_username_key"
DETAIL:  Key (username)=(redacted.redacted) already exists.
) was the direct cause of the following exception:
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/registration/views.py", line 53, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
    return self.form_valid(form)
  File "/usr/local/lib/python3.7/site-packages/registration/views.py", line 56, in form_valid
    new_user = self.register(form)
  File "/usr/local/lib/python3.7/site-packages/registration/backends/default/views.py", line 100, in register
    request=self.request,
  File "/usr/local/lib/python3.7/site-packages/registration/models.py", line 184, in create_inactive_user
    new_user.save()
  File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 740, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 778, in save_base
    force_update, using, update_fields,
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 881, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 921, in _do_insert
    using=using, raw=raw,
  File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /accounts/register/
Exception Value: duplicate key value violates unique constraint "front_user_username_key"
DETAIL:  Key (username)=(redacted.redacted) already exists.

aber wir fangen das eigentlich schon ab:

web/apps/front/forms.py

Lines 48 to 50 in f44e535

# Ensure that user with this email does not exist yet
if User.objects.filter(email__iexact=email).exists():
raise forms.ValidationError("Benutzer mit dieser E-Mail existiert bereits.")

und ich kann das Problem auch nicht reproduzieren - mit denselben Daten wie im Mail kriege ich wie erwartet direkt im Browser eine Meldung. Der User existiert tatsächlich mit der exakt selben Mail schon in der Datenbank.

@dbrgn du hast die Validierung damals™ in b6c0c77 implementiert - kannst du dir da irgend nen Reim daraus machen?

@dbrgn
Copy link
Member

dbrgn commented Aug 26, 2022

Beim Check wird die E-Mail geprüft, bei der Fehlermeldung geht es um den Username. Der wird irgendwie von der E-Mail abgeleitet, oder? Ev gibt's User, wo der Username nicht (mehr) zur E-Mail passt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants