如何调试" django.db.utils.IntegrityError:UNIQUE约束失败"?

时间:2018-05-31 10:56:15

标签: django python-3.x django-models django-errors

我无法调试此内容:

  

django.db.utils.IntegrityError:UNIQUE约束失败:post_employee.employee_id

我通过内置的管理员节省了价值,然后它工作正常,但是当我编辑这些值时,它会引发integrityError。这是我的代码:

models.py

from django.db import models
from django.db.models.signals import post_save
# Create your models here.
class Person(models.Model):
    name = models.CharField(max_length=100)
    gender = models.CharField(max_length=10)

    def __str__(self):
        return (self.name)

class Employee(models.Model):
    employee = models.OneToOneField(Person,on_delete=models.CASCADE)
    emp_no = models.CharField(max_length=100)
    desgnation = models.CharField(max_length=100)

    def __str__(self):
        return str(self.employee)



def create_profile(sender,**kwargs):
    if kwargs:
        Employee.objects.create(employee = kwargs['instance'])
post_save.connect(create_profile,sender=Person)

错误

IntegrityError at /admin/post/person/1/change/
UNIQUE constraint failed: post_employee.employee_id
Request Method: POST
Request URL:    http://localhost:8000/admin/post/person/1/change/
Django Version: 2.0.5
Exception Type: IntegrityError
Exception Value:    
UNIQUE constraint failed: post_employee.employee_id

在数据库中,我看到employee_id - integer - employee_id NOT NULL UNIQUE

这个UNIQUE会出现这个IntegerityError,那么如何纠正呢?我想删除此完整性错误。

0 个答案:

没有答案