0

I have following model and after Migrate, I tried to add data through Admin page.
However, I got the error NOT NULL constraint failed: HVAC_chiller.basicinfo_ptr_id.
Referring to other stackoverflow, I understood problem. However, I cannot find any model which has attribute ptr. Does anyone know which attribute I should revise?

models.py

class BasicInfo(models.Model):
    title = models.CharField(max_length=100)
    manufacturer = models.CharField(max_length=50, blank = True)
    used_project = models.CharField(max_length=50, null=True,blank=True)
    cost=models.IntegerField(null = True, blank = True)
    url=models.URLField(null=True,blank=True)
    comment=models.TextField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

class VRV(BasicInfo):
    InletT = models.FloatField(blank=True)
    OutletT = models.FloatField(blank=True)
    Capacity = models.IntegerField(blank=True)  # Reference capacity
    COP = models.FloatField(blank=True)  # Reference COP

class CapacityFunction(models.Model):
    name=models.CharField(max_length=100, blank = True)
    c1=models.FloatField()
    c2=models.FloatField()
    c3 = models.FloatField()
    c4 = models.FloatField()
    c5 = models.FloatField()
    c6 = models.FloatField()
    minX= models.FloatField(blank=True)
    maxX = models.FloatField(blank=True)
    minY = models.FloatField(blank=True)
    maxY = models.FloatField(blank=True)

    def __str__(self):
        return self.name

class EIRofTemp(CapacityFunction):
    pass

class EIRofPLR(models.Model):
    name = models.CharField(max_length=100, blank=True)
    c1=models.FloatField()
    c2= models.FloatField()
    c3 = models.FloatField()
    c4 = models.FloatField(blank=True,null=True)
    minX = models.FloatField(blank=True)
    maxX = models.FloatField(blank=True)

    def __str__(self):
        return self.name

class Chiller(VRV):
    CONDENSER_CHOICES = (
        ('WaterCooled','WaterCooled'),
        ('AirCooled', 'AirCooled'),
        ('EvaporativelyCooled','EvaporativelyCooled')
    )
    Condenser=models.CharField(max_length=15,choices=CONDENSER_CHOICES,default='Water')
    CHWFlowRate=models.FloatField(blank=True,null=True)
    CWFlowRate=models.FloatField(blank=True,null=True)
    minPLR=models.FloatField(blank=True,null=True)
    maxPLR=models.FloatField(blank=True,null=True)
    optimumPLR=models.FloatField(blank=True,null=True)
    minUnloadRatio=models.FloatField(blank=True,null=True)
    CapacityFunction=models.OneToOneField(CapacityFunction,blank=True,null=True,on_delete=models.CASCADE,related_name='cap')
    EIRofTemp=models.OneToOneField(EIRofTemp,blank=True,null=True,on_delete=models.CASCADE,related_name='eirtemp')
    EIRofPLR = models.OneToOneField(EIRofPLR, blank=True,null=True, on_delete=models.CASCADE,related_name='eirplr')
Katsuya Obara
  • 903
  • 3
  • 14
  • 30
  • Maybe [this ticket](https://code.djangoproject.com/ticket/20113) will help you? _"naming of the (generated) primary / foreign key field of the inheriting table becomes place_ptr_id"_ – Laurynas Tamulevičius Jan 27 '18 at 10:20
  • 2
    You've got a *lot* of multi-table inheritance going on here, which is rarely a good idea. Chiller inherits from VRV which in turn inherits from BasicInfo - that's three separate tables. Are you sure that's what you want? – Daniel Roseman Jan 27 '18 at 10:25

1 Answers1

1

You have tried multi-inheritance. Try to use ForeignKey()

class BasicInfo(models.Model):
    title = models.CharField(max_length=100)
    manufacturer = models.CharField(max_length=50, blank = True)
    used_project = models.CharField(max_length=50, null=True,blank=True)
    cost=models.IntegerField(null = True, blank = True)
    url=models.URLField(null=True,blank=True)
    comment=models.TextField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

class VRV(models.Model):
    basic_info = models.ForeignKey(BasicInfo) # Foreign Key on BasicInfo model
    InletT = models.FloatField(blank=True)
    OutletT = models.FloatField(blank=True)
    Capacity = models.IntegerField(blank=True)
    COP = models.FloatField(blank=True)

class Chiller(models.Model):
    CONDENSER_CHOICES = (
        ('WaterCooled','WaterCooled'),
        ('AirCooled', 'AirCooled'),
        ('EvaporativelyCooled','EvaporativelyCooled'))
    vrv = models.ForeignKey(VRV) # Foreign Key of VRV model
    Condenser=models.CharField(max_length=15, choices=CONDENSER_CHOICES, default='Water')
    CHWFlowRate=models.FloatField(blank=True,null=True)
    CWFlowRate=models.FloatField(blank=True,null=True)
    minPLR=models.FloatField(blank=True,null=True)
    maxPLR=models.FloatField(blank=True,null=True)
    optimumPLR=models.FloatField(blank=True,null=True)
    minUnloadRatio=models.FloatField(blank=True,null=True)
    CapacityFunction=models.OneToOneField(CapacityFunction,blank=True,null=True,on_delete=models.CASCADE,related_name='cap')
    EIRofTemp=models.OneToOneField(EIRofTemp,blank=True,null=True,on_delete=models.CASCADE,related_name='eirtemp')
    EIRofPLR = models.OneToOneField(EIRofPLR, blank=True,null=True, on_delete=models.CASCADE,related_name='eirplr')

Try to use standard Django/Python syntax conventions. for example,

  1. Use lowercase for defining a variable.
  2. Define CHOICES outside of class.
Dhaval Savalia
  • 495
  • 5
  • 17