Kingsley
Kingsley

Reputation: 805

IntegrityError Null constraint violation

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)

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).

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?

Upvotes: 0

Views: 363

Answers (1)

Arpit Svt
Arpit Svt

Reputation: 1203

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.

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

Something like this:

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

# End

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

Upvotes: 1

Related Questions