FOREIGN KEY 约束失败。 EmailAddress 中的 Django allauth 错误
FOREIGN KEY constraint failed. Django allauth error in EmailAddress
我在我的 Django 应用程序中使用 allauth。我有自定义用户模型和自定义注册表单。起初,Model 和 Form 在名为“main”的应用程序中,然后我创建了一个名为“user”的新应用程序并将它们移动到新应用程序。现在我在注册新用户时收到 FOREIGN KEY constraint failed 错误。
我的settings.py里有这个:
AUTH_USER_MODEL = "user.Account"
ACCOUNT_FORMS = {
"signup": "user.forms.CustomSignUpForm",
}
我的自定义注册表单:
class CustomSignUpForm(SignupForm):
full_name = forms.CharField(max_length=350, label="Full Name", required=True)
def save(self, request):
user = super(CustomSignUpForm, self).save(request)
user.full_name = self.cleaned_data['full_name']
user.save()
return user
我的models.py:
class AccountManager(BaseUserManager):
def create_user(self, email, full_name, password=None):
if not email:
raise ValueError("Users must have email address")
if not full_name:
raise ValueError("Users must have name")
user = self.model(
email=self.normalize_email(email),
full_name=full_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, full_name, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
full_name=full_name,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
GENDERS = (
("M", "Male"),
("FM", "Female"),
("O", "Other")
)
SPECIALIZATIONS = (
("DV", "Developer"),
("DS", "Designer"),
)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
full_name = models.CharField(max_length=350)
gender = models.CharField(max_length=50, choices=GENDERS)
birth_date = models.DateField(null=True, blank=True)
specialization = models.CharField(max_length=250, choices=SPECIALIZATIONS)
extra_info = models.BooleanField(default=False)
contacts = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True, verbose_name="date join")
last_login = models.DateTimeField(auto_now=True, verbose_name="last login")
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser= models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['full_name']
objects = AccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
错误:
IntegrityError at /accounts/signup/
FOREIGN KEY constraint failed
错误来自这些代码片段(allauth 源代码):
... self.user = form.save(self.request)
... user = super(CustomSignUpForm, self).save(request)
... setup_user_email(request, user, [])
... a.save()
当我检查“a.save()”附近的代码和局部变量时,我看到了这个:
a: <EmailAddress: emuradyan07@gmail.com>
addresses, primary = cleanup_email_addresses(
request, priority_addresses + addresses
)
for a in addresses:
a.user = user
a.save()
EmailAddress.objects.fill_cache_for_user(user, addresses)
...
在我看来,allauth 在创建新用户时似乎无法创建 EmailAddress。
我找到了解决方案:
- 从应用中删除迁移和 pycache 文件夹
- 删除数据库(db.sqlite3)
- 创建 db.sqlite3
python manage.py makemigrations user
,其中用户是应用名称
python manage.py migrate
我在我的 Django 应用程序中使用 allauth。我有自定义用户模型和自定义注册表单。起初,Model 和 Form 在名为“main”的应用程序中,然后我创建了一个名为“user”的新应用程序并将它们移动到新应用程序。现在我在注册新用户时收到 FOREIGN KEY constraint failed 错误。
我的settings.py里有这个:
AUTH_USER_MODEL = "user.Account"
ACCOUNT_FORMS = {
"signup": "user.forms.CustomSignUpForm",
}
我的自定义注册表单:
class CustomSignUpForm(SignupForm):
full_name = forms.CharField(max_length=350, label="Full Name", required=True)
def save(self, request):
user = super(CustomSignUpForm, self).save(request)
user.full_name = self.cleaned_data['full_name']
user.save()
return user
我的models.py:
class AccountManager(BaseUserManager):
def create_user(self, email, full_name, password=None):
if not email:
raise ValueError("Users must have email address")
if not full_name:
raise ValueError("Users must have name")
user = self.model(
email=self.normalize_email(email),
full_name=full_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, full_name, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
full_name=full_name,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
GENDERS = (
("M", "Male"),
("FM", "Female"),
("O", "Other")
)
SPECIALIZATIONS = (
("DV", "Developer"),
("DS", "Designer"),
)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
full_name = models.CharField(max_length=350)
gender = models.CharField(max_length=50, choices=GENDERS)
birth_date = models.DateField(null=True, blank=True)
specialization = models.CharField(max_length=250, choices=SPECIALIZATIONS)
extra_info = models.BooleanField(default=False)
contacts = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True, verbose_name="date join")
last_login = models.DateTimeField(auto_now=True, verbose_name="last login")
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser= models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['full_name']
objects = AccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
错误:
IntegrityError at /accounts/signup/
FOREIGN KEY constraint failed
错误来自这些代码片段(allauth 源代码):
... self.user = form.save(self.request)
... user = super(CustomSignUpForm, self).save(request)
... setup_user_email(request, user, [])
... a.save()
当我检查“a.save()”附近的代码和局部变量时,我看到了这个:
a: <EmailAddress: emuradyan07@gmail.com>
addresses, primary = cleanup_email_addresses(
request, priority_addresses + addresses
)
for a in addresses:
a.user = user
a.save()
EmailAddress.objects.fill_cache_for_user(user, addresses)
...
在我看来,allauth 在创建新用户时似乎无法创建 EmailAddress。
我找到了解决方案:
- 从应用中删除迁移和 pycache 文件夹
- 删除数据库(db.sqlite3)
- 创建 db.sqlite3
python manage.py makemigrations user
,其中用户是应用名称python manage.py migrate