Foreingkey leave blank or null

Hi all

I have a developed model where I have 2 foreing keys. Depending on a condition, one must be equal to blank and the other must be assigned the corresponding key.

But I get this output:

django.db.utils.IntegrityError: NOT NULL constraint failed: callserviceapp_ordengeneral.rubro_company_id

This is the model:

As you can see, rubro_company has blank = True and null = True

class ordenGeneral (models.Model):

    id = models.AutoField(primary_key=True)
    client_email = models.EmailField(blank=True)
    proveedor_email=models.EmailField(blank=True)

    rubro= models.ForeignKey(item,blank=True,default=None,on_delete=models.CASCADE)

    rubro_company = models.ForeignKey(item_company,blank=True,null=True,on_delete=models.CASCADE)

    ticket =  models.IntegerField(default=1000, blank=True)
    location_lat = models.FloatField(default=None, blank=True)
    location_long = models.FloatField(default=None, blank=True)
    day = models.TextField(default="Lunes Martes Miercoles Jueves Viernes")
    time = models.TimeField(default=None, blank=True)
    tituloPedido = models.TextField(default="Solicitud de pedido")
    problem_description = models.TextField(default=None, blank=True)
    picture1=models.ImageField(default=None, blank=True)
    picture2=models.ImageField(default=None,blank=True)
    presupuesto_inicial=models.FloatField(default=0)
    pedido_mas_información=models.TextField(default=0)
    respuesta_cliente_pedido_mas_información=models.TextField(default=0)
    picture1_mas_información=models.ImageField(default=None, blank=True)
    picture2_mas_información=models.ImageField(default=None,blank=True)
    motivo_rechazo=models.TextField(default=None, blank=True)
    califico_el_cliente=models.BooleanField(default=False)
    califico_el_proveedor=models.BooleanField(default=False)
    calificacion_proveedor= models.IntegerField(default=0)
    calificacion_cliente=models.IntegerField(default=0)
    resena_al_proveedor=models.TextField(default = None, blank=True)    
    resena_al_cliente=models.TextField(default = None, blank=True)

This is the function that adds a new row to the database. With the line #new.rubro_company (its a comment)


                        new=ordenGeneral()
                        new.rubro=rubro
                        #new.rubro_company
                        new.location_lat= clienteLat
                        new.location_long=clienteLong
                        new.tituloPedido=tituloPedido
                        if diaPedido and diaPedido!=None:
                            new.day=diaPedido
                        else:
                            new.day=datetime.date(1997, 10, 19)
                        if horaPedido and horaPedido!=None:
                            new.time=horaPedido
                        else:
                            new.time=datetime.time(0, 0, 0)
                        new.problem_description=descripcion_problema
                        new.direccion=direccion_pedido
                        if request.FILES.get("imagen1"):
                            new.picture1= request.FILES.get("imagen1")
                        if request.FILES.get("imagen2"):
                            new.picture2=request.FILES.get("imagen2")
                        new.client_email=clienteEmail
                        new.proveedor_email=ProveedorEmail
                   ticket_numero=ordenGeneral.objects.count()+ordenEmergencia.objects.count()+1000
                        new.ticket=ticket_numero
                        new.motivo_rechazo=""
                        new.resena_al_proveedor=""
                        new.resena_al_cliente=""
                        new.save()

Is this a recent change to your code to allow this? (Have you done the makemigrations / migrate on this since setting this to allow nulls?)

Thanks Ken, Yes I did.

If that’s not the issue, I think we’re going to need to see the complete traceback. I’m wondering if there’s another issue that’s the actual root cause.

Also, what database are you using? What versions of Django and Python?
(I am unable to recreate this behavior on PostgreSQL with either Django 3.2 / Python 3.9 or Django 4.0 / Python 3.10)