Home > Software design >  Django NOT NULL constraint error on imaginary field
Django NOT NULL constraint error on imaginary field

Time:12-01

I have been getting the following error.

django.db.utils.IntegrityError: NOT NULL constraint failed: doctor_owner.doc_name

This error primarily arises on when I save the owner information using .save() and the error it gives is on doc_name, which is not present in the model definition of the class Owner. I am clueless why it is giving such an error.

My model is attached below: .

This is my model description:

from django.db import models

# Create your models here.
from base.models import BaseModel


class Owner(BaseModel):
    owner_id = models.CharField(max_length=50)
    owner_name = models.CharField(max_length=250)


class Pet(BaseModel):
    owner = models.ForeignKey(Owner, on_delete=models.CASCADE)
    pet_name = models.CharField(max_length=100)
    pet_age = models.DecimalField(max_length=3, decimal_places=2, max_digits=50)
    pet_specie = models.CharField(max_length=250)
    pet_gender = models.CharField(max_length=1)


class Medicine(BaseModel):
    medicine_name = models.CharField(max_length=250)
    frequency = models.CharField(max_length=100)
    duration = models.CharField(max_length=100)


class Prescription(BaseModel):
    pet = models.ForeignKey(Pet, on_delete=models.CASCADE)
    medicine = models.ForeignKey(Medicine, on_delete=models.CASCADE)


class Treatment(BaseModel):
    pet = models.ForeignKey(Pet, on_delete=models.CASCADE)
    owner = models.ForeignKey(Owner, on_delete=models.CASCADE)
    doc_name = models.CharField(max_length=250)
    prescription = models.ForeignKey(Prescription, on_delete=models.CASCADE)

CodePudding user response:

In your treatment table you have a reference as a foreign key to owner; try putting an equivalent to 'nullable=True' or give it a default value

CodePudding user response:

why not put
doc_name = models.CharField(max_length=250, null = True) to try is working

  • Related