本文介绍了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外键不匹配错误吗?

Why am I getting a Django SQLite "foreign key mismatch" error when executing that script?

编译器说

foreign密钥不匹配-引用 item_info的 pur_history

foreign key mismatch - "pur_history" referencing "item_info"

推荐答案

尝试将 to_field 参数添加到像这样的 pur_history 类中的字段商品

Try to add "to_field" argument to field "item" in "pur_history" class like this:

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

I希望这项工作对我有用

I hope this works, it worked for me

这篇关于Django SQLite外键不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:24