Django SQLite外键不匹配错误

时间:2018-05-10 06:49:40

标签: django sqlite

class item_sort_info(models.Model):
    sort = models.CharField(max_length=100, primary_key=True, null=False)


class items(models.Model):
    name = models.CharField(max_length=15, null=False, primary_key=True)
    inventory = models.PositiveSmallIntegerField(null=False)
    price = models.PositiveIntegerField(null=False)
    sort = models.ForeignKey(item_sort_info, on_delete=models.CASCADE)


class item_info(models.Model):
    serial_num = models.PositiveIntegerField(null=False, primary_key=True)
    items = models.ForeignKey(items, on_delete=models.CASCADE)
    inbound_date = models.DateField(auto_now_add=True)


class pur_history(models.Model):
    customer = models.ForeignKey(customer_info, on_delete=models.CASCADE)
    time = models.DateTimeField(auto_now_add=True)
    item = models.OneToOneField(item_info, on_delete=models.CASCADE)

为什么我会收到Django SQLite"外键不匹配"执行该脚本时出错?

编译说

外键不匹配 - " pur_history"引用" item_info"

1 个答案:

答案 0 :(得分:0)

尝试将“ to_field” 参数添加到“ pur_history” 类的字段“ item” 中,

item = models.OneToOneField(item_info, to_field='serial_num', on_delete=models.CASCADE)

我希望这能对我有用