[英]Django 3.0.3 IntegrityError FOREIGN KEY constraint failed when making changes to db
I have two models in my database, an 'Account' model (which is a custom user model) and a 'Return' model.我的数据库中有两个模型,一个“帐户”model(这是一个自定义用户模型)和一个“返回”model。
My database worked fine up to the point of adding the 'Return' model, which has a ForeignKey relationship with the 'User' model, which I suspect is causing the problem.我的数据库运行良好,直到添加了“返回”model,它与“用户”model 有 ForeignKey 关系,我怀疑这是导致问题的原因。 (Each return belongs to an existing user. In the admin panel, the option box is populated with existing users so I thought this was working correctly?) (每个回报都属于现有用户。在管理面板中,选项框填充了现有用户,所以我认为这工作正常?)
Appreciate any help with this!感谢您对此的任何帮助!
Error:错误:
IntegrityError at /admin/returns/return/add/ /admin/returns/return/add/ 处的 IntegrityError
FOREIGN KEY constraint failed FOREIGN KEY 约束失败
Request Method: POST Request URL: http://127.0.0.1:8000/admin/returns/return/add/ Django Version: 3.0.3 Exception Type: IntegrityError Exception Value: Request Method: POST Request URL: http://127.0.0.1:8000/admin/returns/return/add/ Django Version: 3.0.3 Exception Type: IntegrityError Exception Value:
FOREIGN KEY constraint failed FOREIGN KEY 约束失败
Here is my Return app's models.py:这是我的 Return 应用程序的 models.py:
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class Return(models.Model):
user = models.ForeignKey(User, related_name="returns", on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now=True)
last_edited = models.DateTimeField(null=True)
TAX_YEAR_CHOICES = [
('2019', '2019'),
('2020', '2020'),
('2021', '2021'),
]
tax_year_ending = models.CharField(
choices=TAX_YEAR_CHOICES,
max_length=4,
blank=False,
null=False,
)
RETURN_STATUS_CHOICES = [
('1', 'In progress'),
('2', 'Completed, awaiting review'),
('3', 'Completed, under review'),
('4', 'Completed, awaiting client action'),
('5', 'Submitted to HMRC'),
]
return_status = models.CharField(
choices=RETURN_STATUS_CHOICES,
max_length=1,
default=1,
blank=False,
null=False,
)
Here is my custom user class (also setup in settings.py):这是我的自定义用户 class(也在 settings.py 中设置):
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True) # tells Django this is the unique ID
#following required for the AbstractBaseUser class
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
#tell django we want to use email as our username field (for logins)
USERNAME_FIELD = 'email'
#tells Django to user AccountManager
objects = AccountManager()
#tell django which fields are required on registration:
#REQUIRED_FIELDS = []
#what to return when we print the Account object
def __str__(self):
return self.email
#following required for AbstractBaseUser class
#does the user have permissions, yes if is_admin
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
Here is the post request information from the errog log: user这是来自错误日志的发布请求信息:用户
'3' '3'
last_edited_0 last_edited_0
'2020-05-03' '2020-05-03'
last_edited_1 last_edited_1
'13:01:23' '13:01:23'
tax_year_ending tax_year_ending
'2019' '2019'
return_status返回状态
'3' '3'
_addanother _加上另一个
'Save and add another' '保存并添加另一个'
(I have confirmed that there is a user with pk '3') (我已经确认有一个 pk '3' 的用户)
Thanks again, really appreciate it.再次感谢,真的很感激。
You need to record the instance of user on behalf of pk number:您需要代表 pk 号记录用户的实例:
User.objects.get(pk=3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.