IntegrityError空约束违反 [英] IntegrityError Null constraint violation

查看:103
本文介绍了IntegrityError空约束违反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django应用程序中有三个模型...一个成员模型,一个应用程序模型和一个应用程序审查模型。

I have three models in my django app...a members model, an application model and an applications review model.

我的成员模型如下所示:

My members model looks like this...

class Members(models.Model):
    TITLES = (
        ('chairman', 'Chairman'),
        ('secretary', 'Secretary')
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=10, choices=TITLES, default='secretary')

我的应用程序模型...

My Applications model...

class Application(models.Model):
   firstname = models.CharField(max_length=20)
   middlename = models.CharField(max_length=20)
   lastname = models.CharField(max_length=20)
   dob = DateField()

应用程序审查模型...

The applications review model...

class ApplicationsReview(models.Model):
    APPLICATION_STATUS = (
        ('pending', 'Pending Review'),
        ('approved', 'Approved'),
        ('rejected', 'Rejected')
    )
    applicant = models.OneToOneField(Application, on_delete=models.CASCADE, primary_key=True)
    chairman = models.ForeignKey(Members, related_name='chairs', on_delete=models.CASCADE)
    secretary = models.ForeignKey(Members, related_name='secretaries', on_delete=models.CASCADE)
    application_status = models.CharField(max_length=10, choices=APPLICATION_STATUS, default='pending')
    status_justification = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

创建应用程序后,我想它的审查也会实例化,因此,在应用程序审查模型的正下方有以下信号...

When an application is created, I would like its review instantiated as well, hence, I have the following signal right below the applications review model...

# When an application is created, create with it an application review and associate it with the application instance

@receiver(post_save, sender=Application)
def create_application_review(sender, **kwargs):
    instance = kwargs['instance']
    created = kwargs['created']
    if created:
        ApplicationReview.objects.create(applicant=instance)

但是,当我尝试在django管理员中添加应用程序,我得到错误

However, when I try to add an application in django admin I get the error

null value in column "chairman_id" violates not-null constraint
DETAIL:  Failing row contains (3, pending, 2019-02-08 03:26:04.643452+00, null, null).

错误似乎是由于试图实例化的信号导致的ApplicationsReview 实例,但不提供主席和秘书的值。即使将其设置为允许空字段也无法消除错误。我在这里想念什么吗?

The error seems to be as a result of the signal trying to instantiate an ApplicationsReview instance without providing the values for the chairman and secretary. Even setting those to allow null fields doesn't get rid of the error. Is there something I'm missing here?

推荐答案

创建 ApplicationsReview 要求您传递以下详细信息-主席,秘书,status_justification ,但是在信号中创建 ApplicationReview 时,您只是传递了 applicant ,因此Django假设主席,秘书,status_justification 字段的值为Null,这就是您收到此错误的原因。

Creating ApplicationsReview requires you to pass the following details - chairman, secretary, status_justification But while creating ApplicationReview in the signal you are just passing value of applicant, So Django is assuming the values of chairman, secretary, status_justification fields as Null that is why you are getting this error.

如果要将这些字段设置为非强制性,则可以在定义模型中的字段时传递 null = True,Blank = True

If you want to make these field Non-compulsory you can pass null=True, Blank=True while defining the field in the model.

类似这样的东西:

chairman = models.ForeignKey(Members, null=True, blank=True, related_name='chairs', on_delete=models.CASCADE)

# End

您可以参考此答案以进一步了解何时使用 null = True blank = True 或两者兼而有之。
https://stackoverflow.com/a/8609425/6280433

You can refer this answer to get more understanding of when to use null=True, blank=True or both. https://stackoverflow.com/a/8609425/6280433

这篇关于IntegrityError空约束违反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆