Django-allauth: UNIQUE constraint failed: auth_user.username with registering with email

Created on 3 Jul 2015  路  9Comments  路  Source: pennersr/django-allauth

I've switched to using email based registration and am facing an issue. After my initial registration test, I see the username name is blank. After trying to register another user I got the following error:

UNIQUE constraint failed: auth_user.username

My settings making allauth register using emails

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'

I've implemented allauth in projects before and it would insert the first name into the username fields( allauth 19.x). Now using .20 and it doesn't seem to work the same way. Any help is greatly appreciated.

Most helpful comment

Update:

Looks like it works with settings like:
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True

In this case it creates different usernames in auth_user, but doesn't ask user to provide username explicitly. Though it is not very clear from documentation.

All 9 comments

By configuring ACCOUNT_USER_MODEL_USERNAME_FIELD = None you are telling allauth that no username field exists. Hence, no attempt is made tot fill it in.

Ok, I've tried:

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 7
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True

and it still puts in a blank username. I've also tried setting the ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email' and that too is blank...what am I missing here?

Setting ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email' wrong, it should be set to ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username' or whathever your username field is named.

ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username' is the default so if I omit it, why is it still blank? From using allauth previously, it used the first name field as a username and appended a number to it to keep it unique if another record had the same first_name. Using .20 it doesn't seem to do that anymore(was using .19.1 in my last project). I appreciate the support! FYI this is on django 1.8 using python 3.4

Likely because your custom signup form offers a username field, which it should not, as allauth already handles this.

Hello, facing the same issue with 0.20 and 0.21 (not tested on earlier though).

I've got Facebook configured as a provider and tested following configurations:

ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['public_profile', 'email', 'publish_actions'],
'METHOD': 'js_sdk', # instead of 'oauth2'
'VERIFIED_EMAIL': False # suppress explicit request to confirm email of Facebook user
}
}

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['public_profile', 'email', 'publish_actions'],
'METHOD': 'js_sdk', # instead of 'oauth2'
'VERIFIED_EMAIL': False # suppress explicit request to confirm email of Facebook user
}
}

When I first sign up as a Facebook user, django-allauth creates user with empty username in auth_user table.
Then, I create another user with email auth, form doesn't contain Username field and it generates an exception (IntegrityError at /accounts/signup/
UNIQUE constraint failed: auth_user.username).
Obviously, it tries to create another user with empty username in auth_user table and fails.

Update:

Looks like it works with settings like:
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True

In this case it creates different usernames in auth_user, but doesn't ask user to provide username explicitly. Though it is not very clear from documentation.

I had to end up using the same settings as lukovkin

same here solution here too.

Was this page helpful?
0 / 5 - 0 ratings