简体   繁体   English

外键与Django Model中的相关字段发生冲突

[英]Foreign Keys clash with related field in Django Model

I'm working on a model class that will represent the relationship of one family member to another (part of a geneology feature) 我正在研究一个模型类,它将代表一个家庭成员与另一个家庭成员的关系(一部分基因学特征)

My Class is: 我的班级是:

class FamilyLink(models.Model):
    from_legacy = models.ForeignKey(Legacy)
    to_legacy = models.ForeignKey(Legacy)

    class Meta:
        unique_together = ("from_legacy", "to_legacy")

When I try and migrate I get the following error message: 当我尝试迁移时,我收到以下错误消息:

CommandError: One or more models did not validate: archive.familylink: Accessor for field 'from_legacy' clashes with related field 'Legacy.familylink_set'. CommandError:一个或多个模型未验证:archive.familylink:字段'from_legacy'的访问者与相关字段'Legacy.familylink_set'冲突。 Add a related_name argument to the definition for 'from_legacy'. 将related_name参数添加到'from_legacy'的定义中。 archive.familylink: Accessor for field 'to_legacy' clashes with related field 'Legacy.familylink_set'. archive.familylink:字段'to_legacy'的访问者与相关字段'Legacy.familylink_set'发生冲突。 Add a related_name argument to the definition for 'to_legacy'. 将related_name参数添加到'to_legacy'的定义中。

It seem my issue is having two foreignKey's in the same class both pointing to the same class (in this case the "Legacy" class). 看来我的问题是在同一个类中有两个foreignKey都指向同一个类(在本例中是“Legacy”类)。 Does anyone know how I can be resolve/work around this? 有谁知道如何解决/解决这个问题?

I appreciate the thoughts and expertise. 我很欣赏这些想法和专长。

The error message is quite explanatory: 错误消息非常明确:

class FamilyLink(models.Model):
    from_legacy = models.ForeignKey(Legacy, related_name = 'familylink_from_legacy')
    to_legacy = models.ForeignKey(Legacy, related_name = 'familylink_to_legacy')

By default, if no related_name attribute is set, the relatedname is set to familylink_set and since 2 different fields from the same relation, it causes the issues. 默认情况下,如果没有related_name属性被设置,relatedname设置为familylink_set以及由于来自同一关系2个不同的领域,它导致的问题。

Read more on related_name attribute here 此处阅读related_name属性的更多信息

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

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