【问题标题】:Multiple foreign keys to the same id. Django. Design Patterns同一个 id 的多个外键。姜戈。设计模式
【发布时间】:2015-05-07 12:03:55
【问题描述】:

我真的不明白为什么我不能多次用 Foregin Key 指向完全相同的 id。

我正在尝试对已经存在的数据库使用 Django ORM。 它看起来像这样:

我想根据这个来创建模型:

 class TestID(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=250)
    test_case_id = models.CharField(max_length=250, unique=True)
    module = models.CharField(max_length=50)
    full_description = models.TextField()

    class Meta:
        db_table = "TestID"

class TestCaseRun(models.Model):
    id = models.AutoField(primary_key=True)
    soft_version = models.CharField(max_length=50)
    automated_test_case_version = models.CharField(max_length=50, unique=True)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    checksum = models.CharField(max_length=250)
    result = models.CharField(max_length=50)
    test_case_id = models.ForeignKey(TestID, db_column='test_case_id')

    class Meta:
        db_table = "TestCaseRun"

class TestStep(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=250, null=False)
    result = models.CharField(max_length=50)
    description = models.CharField(max_length=250)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    test_case = models.ForeignKey(TestCaseRun, db_column='id')

    class Meta:
        db_table = "TestStep"



class single_check(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=250)
    comparison = models.CharField(max_length=5, null=False)
    expected = models.CharField(max_length=50, null=False)
    actual = models.CharField(max_length=50, null=False)
    result = models.CharField(max_length=50)
    comment = models.CharField(max_length=250)
    event_time = models.DateTimeField()
    test_step_id = models.ForeignKey(TestStep, db_column='id')

    class Meta:
        db_table = "single_check"

class action(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=250)
    type = models.CharField(max_length=5, null=False)
    result = models.CharField(max_length=50)
    comment = models.CharField(max_length=250)
    event_time = models.DateTimeField()
    test_step_id = models.ForeignKey(TestStep, db_column='id')

    class Meta:
        db_table = "action"

class logs(models.Model):
    id = models.AutoField(primary_key=True)
    msg = models.CharField(max_length=350)
    type = models.CharField(max_length=5, null=False)
    result = models.CharField(max_length=50)
    comment = models.CharField(max_length=250)
    event_time = models.DateTimeField()
    test_step_id = models.ForeignKey(TestStep, db_column='id')

    class Meta:
        db_table = "logs"

当我尝试运行该代码时出现错误:

ERRORS:
web_report.TestStep: (models.E007) Field 'test_case' has column name 'id' that is used by another field.
    HINT: Specify a 'db_column' for the field.
web_report.action: (models.E007) Field 'test_step_id' has column name 'id' that is used by another field.
    HINT: Specify a 'db_column' for the field.
web_report.logs: (models.E007) Field 'test_step_id' has column name 'id' that is used by another field.
    HINT: Specify a 'db_column' for the field.
web_report.single_check: (models.E007) Field 'test_step_id' has column name 'id' that is used by another field.
    HINT: Specify a 'db_column' for the field.

我真的不明白为什么我不能多次用 Foregin Key 指向完全相同的 id。恕我直言,这种设计没有任何问题。但我是关系数据库设计的初学者。

【问题讨论】:

  • 提示:为字段指定一个“db_column”...
  • 这与多次使用外键无关。

标签: python sql django orm


【解决方案1】:

您似乎错误地使用了db_column 参数。这是您要链接的模型上的字段,而不是您要链接到的模型上的列。你不能使用db_column='id',因为每个模型已经有一个主键id。

以你的TestStep 模型为例:

class TestStep(models.Model):
    id = models.AutoField(primary_key=True)
    ...
    test_case = models.ForeignKey(TestCaseRun, db_column='id')

您的图表显示链接到TestCase 模型的是test_case_id 列。所以你应该有:

    test_case = models.ForeignKey(TestCaseRun, db_column='test_case_id')

或者因为这是默认设置,所以简单

    test_case = models.ForeignKey(TestCaseRun)

【讨论】:

  • 使用 db_column 也是一个错误。我应该使用'to_field'。 test_case = models.ForeignKey(TestID, to_field='test_case_id')
  • 很高兴你让它工作了,但这不是你的图表显示的 - 它显示test_case链接到TestRun模型。
猜你喜欢
  • 2016-01-18
  • 2015-05-24
  • 2016-04-25
  • 2012-05-29
  • 2019-01-04
  • 1970-01-01
  • 2019-07-12
  • 2014-07-27
  • 2010-12-21
相关资源
最近更新 更多