我真的不明白为什么我不能通过 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论证不正确。这是您要链接的模型上的字段,而不是您要链接到的模型上的列。您不能使用 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) 


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!