Duplicate Email Address in Django AllAuth

So i succesfully signed up a user through Github, via django All-auth

now the problem is when github grants access to the User, it takes them to a page in my website that tells them to sign up using thier github username and email address.

the only problem is that the email address already exists in my database,

so when i click on sign up, it throws a duplicate integrity check error. now how to solve this and make the user automatically login when thier email address already exists in the database.


# IntegrityError at /accounts/social/signup/

duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd.net@gmail.com) already exists.

|Request Method:|POST|
| --- | --- |
|Request URL:||
|Django Version:|4.1.7|
|Exception Type:|IntegrityError|

|Exception Value:|duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd@gmail.com) already exists.|

|Exception Location:|/home/firaki/.local/lib/python3.10/site-packages/django/db/backends/utils.py, line 89, in _execute|

|Raised during:|allauth.socialaccount.views.SignupView|

so the error is raised in this views file


i have located it and i changed the form_valid method, but it still raises the ERROR

Changed it from THIS

    def form_valid(self, form):
        self.request.session.pop("socialaccount_sociallogin", None)
        return helpers.complete_social_signup(self.request, self.sociallogin)


     def form_valid(self, form):
        email = form.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            # Email already exists, log in the user and redirect to home page
            user = User.objects.get(email=email)
                "You were automatically logged in as your email already exists.",
            auth_login(self.request, user, account_settings.AUTHENTICATION_BACKENDS[0])
            return HttpResponseRedirect(reverse('home'))
            # Email does not exist, create a new user
            self.request.session.pop('socialaccount_sociallogin', None)
            return helpers.complete_social_signup(self.request, self.sociallogin)


It sounds like you might be encountering this issue: Integrity error during social login: duplicate key violates unique constraint on user table · Issue #3266 · pennersr/django-allauth · GitHub.

A workaround is to use one of the suggested fixes in your own signup form. E.g.

class CustomSocialSignupForm(SocialSignupForm):
    """Custom social signup form to work around this issue:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.prevent_enumeration = False

Then in settings.py

    "signup": "apps.users.forms.CustomSocialSignupForm",
1 Like

i am sorry , i am a bit confused where should i add this form, or create it in the forms.py file ?

as i dont have a custom sign up form, django alluth does it for me

sorry for the noob question

you can put it anywhere, it just has to be referenced by the same path in settings

i am sorry but the social signup form does not exist

    from allauth.socialaccount.forms import SocialSignupForm
ImportError: cannot import name 'SocialSignupForm' from 'allauth.socialaccount.forms

maybe it has a differnt name ?

alright thanks you so much i solved it :slight_smile:

its SignupForm instead of SocialSignupForm

thanks so much

now only one last thing, i found another buggy error is that

when i sign up normally but use the same email address, it still sends an email address to my email , but the email message is that somebody try to sign up using your email address,

how can i just stop them at the sign up page that the email already exists