【问题标题】:Django ORM error - cannot insert NULL into Primary KeyDjango ORM 错误 - 无法将 NULL 插入主键
【发布时间】:2020-11-22 01:28:23
【问题描述】:

当我尝试使用 Django ORM 创建对象时,Oracle 数据库要求我提供 Primary Key,对应于我的 Orden 模型中的 id_orden 字段,我已将其定义为 IntegerField(primary key=True)。 但是 Django ORM 应该将下一个值插入到数据库中,因为我已经定义了 Primary Key 字段,所以我不明白为什么我仍然被要求提供它,为什么它仍然是空的。

我遇到的错误

IntegrityError at /listado_carro/ ORA-01400: 无法将 NULL 插入 ("SUSHIFU"."MTV_ORDEN"."ID_ORDEN")

发生的错误在views.py的这一行

orden 
=Orden.objects.create(id_carro=carro,fecha_hora=timezone.now(),nombre_cliente=usuario2.nombre+' 
'+usuario2.apellido,direccion=usuario2.direccion,nota='',total=total2)

Models.py

class Orden(models.Model):
    id_orden = models.IntegerField(primary_key=True)
    id_carro = models.OneToOneField('Carro', on_delete=models.CASCADE)
    fecha_hora = models.DateTimeField(default=timezone.now)
    nombre_cliente = models.CharField(max_length=50)
    direccion = models.CharField(max_length=99)
    nota = models.CharField(max_length=200)
    total= models.IntegerField(default='0')

Views.py

def listado_carro(request):
    usuario = request.user
    usuario2 = Usuario.objects.get(id=usuario.id)
    locale.setlocale(locale.LC_ALL, '')
    carros = Carro.objects.filter(id_carro=usuario.id)
    carro = Carro.objects.filter(id_carro=usuario.id).first()    
    total = locale.format('%.0f',
                  Carro.objects.filter(id_carro=usuario.id).aggregate(sum=Sum('precio_producto'))['sum'], 
                                                                           grouping=True, monetary=True)
    total2 = Carro.objects.filter(id_carro=usuario.id).aggregate(sum=Sum('precio_producto'))['sum']
    orden =Orden.objects.create(id_carro=carro,fecha_hora=timezone.now(),
                            nombre_cliente=usuario2.nombre+' '+usuario2.apellido,
                            direccion=usuario2.direccion,nota='',
                            total=total2)
    data = {'carros':carros,'total':total, 'orden':orden}
    return render(request, "listado_carro.html", data)

【问题讨论】:

    标签: python-3.x django


    【解决方案1】:

    您将主键定义为一个整数字段,这意味着您必须为插入语句提供一个 id。相反,您应该使用 AutoField:

    class Orden(models.Model):
        id_orden = models.AutoField(primary_key=True)
        id_carro = models.OneToOneField('Carro', on_delete=models.CASCADE)
        ...
    

    【讨论】:

    • 谢谢,为了正常工作,我必须删除我的 oracle 数据库中的所有表,然后进行 makemigrations 和 migrate,不要只使用 makemigrations 和 migrate。
    【解决方案2】:

    如果您希望 Django ORM 自动增加字段,您需要使用 AutoFieldBigAutoField 而不是 IntegerFieldhttps://docs.djangoproject.com/en/3.1/ref/models/fields/#autofield

    试试:

    class Orden(models.Model):
        id_orden = models.BigAutoField(primary_key=True)
        id_carro = models.OneToOneField('Carro', on_delete=models.CASCADE)
        fecha_hora = models.DateTimeField(default=timezone.now)
        nombre_cliente = models.CharField(max_length=50)
        direccion = models.CharField(max_length=99)
        nota = models.CharField(max_length=200)
        total= models.IntegerField(default='0')
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2013-02-21
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多