2015-05-07 45 views
1

我真的不知道爲什麼我不能指出由Foregin Key多次完全相同的ID。對同一個ID的多個外鍵。 Django的。設計模式

我想使用Django ORM到已經存在的數據庫。 它看起來是這樣的:

enter image description here

我想根據該創建模型:

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。 Imho這個設計沒有錯。但我是關係數據庫設計的初學者。

+0

提示:指定領域的「db_column」 ... – Jingo

+1

此無關使用外鍵多倍。使用db_column的 –

回答

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') 

你的圖表顯示,它是test_case_id列鏈接到TestCase模型。所以,你應該有:

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

或因爲這是默認的,只要

test_case = models.ForeignKey(TestCaseRun) 
+0

也是一個錯誤。我應該使用'to_field'。 'test_case = models.ForeignKey(TestID,to_field ='test_case_id')' – Tomasz

+0

很高興你的工作,但這不是你的圖表顯示 - 它顯示'test_case'鏈接到'TestRun'模型。 – Alasdair

相關問題