UNIQUE constraint failed: auth_user.username with registering with email

南楼画角 提交于 2021-01-28 00:02:20

问题


I am having this weird error after the integration with django-allauth successfully running for several days. My approach is similar to this project. https://github.com/aellerton/demo-allauth-bootstrap

Instead of the user_name, the email is used here as unique identifier of user.

IntegrityError at /accounts/signup/
UNIQUE constraint failed: auth_user.email
Request Method: POST
Request URL: http://localhost:8080/accounts/signup/
Django Version: 1.8
Exception Type: IntegrityError
Exception Value:

UNIQUE constraint failed: auth_user.email
Exception Location: ~/web_dev/unicorn_dev/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py in execute, line 318
Python Executable: ~/web_dev/unicorn_dev/bin/python
Python Version: 3.4.3
Python Path:

['~/web_dev/unicorn_dev/funding',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload',
'~/web_dev/unicorn_dev/lib/python3.4/site-packages']

Here is the Model class:

class AccountUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(('email address'), blank=False, max_length=255, unique=True)
    first_name = models.CharField(('first name'), max_length=40, blank=True, null=True, unique=False)
    last_name = models.CharField(_('last name'), max_length=40, blank=True, null=True, unique=False)

Here is the settings.py

ACCOUNT_USER_MODEL_USERNAME_FIELD = None /* user_name is not used, if not None, migration will report errors */
ACCOUNT_USERNAME_REQUIRED = False
#Enforce uniqueness of e-mail addresses.
ACCOUNT_USER_MODEL_EMAIL_FIELD = "email"
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_AUTHENTICATION_METHOD = "email"

Here is what I did:

1. Deleting the whole database and re-migrating the database does not help at all. 
2. Checked this link https://github.com/pennersr/django-allauth/issues/1014

Could anyone please help to point out possible mistakes? I could not understand why it suddenly reports such errors. Thanks a lot!


回答1:


I finally figured out the causes and list them here. This can help someone who run into the same situation as me.

  1. SITE_ID in settings.py shall be changed to match the site_id in admin
  2. Add the signup in the custom sign up form. Please see here How to customize user profile when using django-allauth


来源:https://stackoverflow.com/questions/33093754/unique-constraint-failed-auth-user-username-with-registering-with-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!