Closed snopoke closed 1 year ago
django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_customuser_username_key"
The above suggests that email is not the root cause of the issue, but username is. How is username populated in your setup?
django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_customuser_username_key"
The above suggests that email is not the root cause of the issue, but username is. How is username populated in your setup?
Ah, I see we also override populate_username
and set the username to the email address. I'll have to disable that and test again to see if this is still an issue.
Closing -- I am fairly sure that allauth works correctly on this point and that the populate_username
is the issue.
django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_customuser_username_key"
The above suggests that email is not the root cause of the issue, but username is. How is username populated in your setup?
Ah, I see we also override
populate_username
and set the username to the email address. I'll have to disable that and test again to see if this is still an issue.
Have you solved the issue? I have the same problem but with duplicated user email: django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_user_email_243f6e77_uniq"
My settings (django-allauth 0.54.0): ACCOUNT_AUTHENTICATION_METHOD = "email" ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_VERIFICATION = "mandatory" ACCOUNT_UNIQUE_EMAIL = True ACCOUNT_PREVENT_ENUMERATION = True
When ACCOUNT_PREVENT_ENUMERATION = False everything works as it should.
This happens only when: 1) User sign up with email & password 2) Next, the same user try to sign up with social login
There is no error, when: 1) User sign up with social login 2) The same user try to sign up with email & password
With the project setup described below, if a user that already has an account attempts to login with a social account (that has the same email) they are redirected to the 'social signup' with their email address pre-populated.
Upon submitting the form they get a 500 error response.
Error traceback:
Diagnosis
allauth.account.forms.BaseSignupForm.clean_email
only raises a ValidationError ifself.prevent_enumeration
is False (which is not the case with the above settings):https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L362-L364
Instead it sets
self.account_already_exists = True
on the form. This is then handled by thesave
method on the subclassed forms e.g. https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L437-L442The problem is that the
allauth.socialaccount.forms.SignupForm
does not handle this condition and attempts to save the user which triggers the IntegrityError: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/forms.py#L26-L30Proposed solutions
self.account_already_exists = True
inallauth.socialaccount.forms.SignupForm
by requiring the user to login with a password and then connect the social account to their existing user.or
self.prevent_enumeration
toFalse
inallauth.socialaccount.forms.SignupForm