简体   繁体   English

Django 多个外键指向同一张表

[英]Django multiple foreign key to a same table

I need to log the transaction of the item movement in a warehouse.我需要在仓库中记录物品移动的交易。 I've 3 tables as shown in the below image.我有 3 个表,如下图所示。 However Django response error:然而 Django 响应错误:

ERRORS: chemstore.ItemTransaction: (models.E007) Field 'outbin' has column name 'bin_code_id' that is used by another field.错误:chemstore.ItemTransaction:(models.E007)字段“outbin”的列名“bin_code_id”被另一个字段使用。

which is complaining of multiple uses of the same foreign key.这是抱怨多次使用同一个外键。 Is my table design problem?我的桌子设计有问题吗? or is it not allowed under Django?还是在 Django 下不允许? How can I achieve this under Django?如何在 Django 下实现这一点? thankyou谢谢你

DB design数据库设计

[Models] [楷模]

class BinLocation(models.Model):
    bin_code = models.CharField(max_length=10, unique=True)
    desc = models.CharField(max_length=50)

    def __str__(self):
        return f"{self.bin_code}"

    class Meta:
        indexes = [models.Index(fields=['bin_code'])]


class ItemMaster(models.Model):
    item_code = models.CharField(max_length=20, unique=True)
    desc = models.CharField(max_length=50)
    long_desc = models.CharField(max_length=150, blank=True)
    helper_qty = models.DecimalField(max_digits=10, decimal_places=4)
    unit = models.CharField(max_length=10, blank=False)

    def __str__(self):
        return f"{self.item_code}"

    class Meta:
        verbose_name = "Item"
        verbose_name_plural = "Items"
        indexes = [models.Index(fields=['item_code'])]


class ItemTransaction(models.Model):
    trace_code = models.CharField(max_length=20, unique=False)
    item_code = models.ForeignKey(
        ItemMaster, related_name='trans', on_delete=models.CASCADE, null=False)
    datetime = models.DateTimeField(auto_now=False, auto_now_add=False)
    qty = models.DecimalField(max_digits=10, decimal_places=4)
    unit = models.CharField(max_length=10, blank=False)
    action = models.CharField(
        max_length=1, choices=ACTION, blank=False, null=False)
    in_bin = models.ForeignKey(
        BinLocation, related_name='in_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
    out_bin = models.ForeignKey(
        BinLocation, related_name='out_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
    remarks = models.TextField(blank=True)

    def __str__(self):
        return f"{self.trace_code} {self.datetime} {self.item_code} {dict(ACTION)[self.action]} {self.qty} {self.unit} {self.in_bin} {self.out_bin}"

you have same db_column in two fields so change it你在两个字段中有相同的db_column所以改变它

    in_bin = models.ForeignKey(
        BinLocation, related_name='in_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
    out_bin = models.ForeignKey(
        BinLocation, related_name='out_logs', db_column='other_bin_code', on_delete=models.CASCADE, null=False) /*change db_column whatever you want but it should be unique*/

If are linked to the same model name, You should use different related_name for each foreign_key filed.如果链接到相同的 model 名称,您应该为每个foreign_key 字段使用不同的related_name。 here is the exemple:这是一个例子:

    address1 = models.ForeignKey(Address, verbose_name=_("Address1"),related_name="Address1", null=True, blank=True,on_delete=models.SET_NULL)

    address2 = models.ForeignKey(Address, verbose_name=_("Address2"),related_name="Address2", null=True, blank=True,on_delete=models.SET_NULL)

thank you for everyone helped.谢谢大家的帮助。 According to Aleksei and Tabaane, it is my DB design issue (broken the RDBMS rule) rather than Django issue.根据 Aleksei 和 Tabaane 的说法,这是我的数据库设计问题(违反了 RDBMS 规则),而不是 Django 问题。 I searched online and find something similar: ONE-TO-MANY DB design pattern我在网上搜索,发现类似的东西: ONE-TO-MANY DB design pattern

In my case, I should store in bin and out bin as separated transaction instead of both in and out in a single transaction.就我而言,我应该将 bin 和 out bin 存储为单独的事务,而不是在单个事务中同时存储 in 和 out。 This is my solution.这是我的解决方案。 thankyou.谢谢你。

ps alternative solution: I keep in bin and out bin as single transaction, but I don't use foreign key for bins, query both in bin and out bin for the bin selection by client application. ps 替代解决方案:我将 bin 和 out bin 保留为单个事务,但我不使用 bin 的外键,同时查询 in bin 和 out bin 以获取客户端应用程序的 bin 选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM