Django中的循环DB关系

时间:2012-10-28 08:36:36

标签: django database relationship circular-dependency

我有以下型号(campaign / models.py)

from django.db import models
from django.contrib.auth.models import User

class Module(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class TestType(models.Model):
    name = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class Test(models.Model):
    PROTO_CHOICES = (
        ('TCP','tcp'),
        ('UDP','udp'),
    )
    module = models.ForeignKey(Module)
    testtype = models.ForeignKey(TestType)
    name = models.CharField(max_length=50, unique=True)
    port = models.IntegerField(blank=True, null=True)
    proto = models.CharField(max_length=3, blank=True, choices=PROTO_CHOICES)
    payload = models.TextField()
    sig_match = models.TextField(blank=True)
    def __unicode__(self):
        return self.name

class Campaign(models.Model):
    name = models.CharField(max_length=50, unique=True)
    tests = models.ManyToManyField(Test)
    updated = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, editable=False)
    def __unicode__(self):
        return self.name

class CampaignRun(models.Model):
    campaign = models.ForeignKey(Campaign)
    name = models.CharField(max_length=50, unique=True)
    run_start = models.DateTimeField()
    run_end = models.DateTimeField()
    updated = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User, editable=False)
    class Meta:
    def __unicode__(self):
        return self.name

class TestRun(models.Model):
    campaignrun = models.ForeignKey(CampaignRun)
    test = models.ForeignKey(Test)
    test_start = models.DateTimeField()
    test_end = models.DateTimeField()
    alert = models.TextField(blank=True)
    flag = models.IntegerField(blank=True, null=True)

...导致以下数据库(sqlite3):

                                                                       +-----------------------------+
                                                                       |                             |
   +---------------------------+     +------------------------------+  |     +--------------------+  |
   | campaigns_testrun         |     | campaigns_test               |  |     | campaigns_module   |  |
   +----------------+----------+     +----------------+-------------+  |     +------+-------------+  |
   | id             | INT      |  +--| id             | INT         |--+  +--| id   | INT         |  |
+--| campaignrun_id | INT      |  |  | module_id      | INT         |-----+  | name | VARCHAR(20) |  |
|  | test_id        | INT      |--+  | testtype_id    | INT         |--+     +------+-------------+  |
|  | test_start     | DATETIME |     | name           | VARCHAR(50) |  |                             |
|  | test_end       | DATETIME |     | port           | INT         |  |     +--------------------+  |
|  | alert          | TEXT     |     | proto          | VARCHAR(3)  |  |     | campaigns_testtype |  |
|  | flag           | INT      |     | payload        | TEXT        |  |     +------+-------------+  |
|  +----------------+----------+     | sig_match      | TEXT        |  +-----| id   | INT         |  |
|                                    +----------------+-------------+        | name | VARCHAR(20) |  |
|                                                                            +------+-------------+  |
|                                                                                                    |
|  +------------------------+                                +--------------------------+            |
|  | campaigns_campaignrun  |     +--------------------+     | campaigns_campaign_tests |            |
|  +-------------+----------+     | campaigns_campaign |     +-------------+------------+            |
+--| id          | INT      |     +------+-------------+     | id          | INT        |            |
   | campaign_id | INT      |-----| id   | INT         |-----| campaign_id | INT        |            |
   | name        | INT      |     | name | VARCHAR(50) |     | test_id     | INT        |------------+
   | run_start   | DATETIME |     +------+-------------+     +-------------+------------+
   | run_end     | DATETIME |     
   +-------------+----------+     

这是逻辑:

  • 广告系列由测试组成
  • 广告系列可以多次投放(campaignrun)并生成测试结果(测试运行)。
  • 对于每个广告系列投放,都会从测试表
  • 收集测试相关信息

显然,存在循环关系,数据库的完整性仅由应用程序确保。

但是,从数据库架构师的角度来看,这种循环关系是不正确的,因为它可能导致完整性问题:

  • 在SQL中直接或从django管理模块中,可以在campaigns_testrun中创建不在campaign_tests表中的测试

由于上述原因,解决方案是直接将campaigns_testrun与campaigns_campaign_tests相关联。但是,我认为Django不可能。

如果你有任何想法如何解决这个问题,欢迎发表评论。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用ManyToManyField through argument

这允许您使用自定义模型,并在FK,M2M等中引用它...

另请注意,您可以引用您的班级名称,即。 ForeignKey('SomeClass'),可以毫无问题地启用循环关系。

欢迎来到Django,享受您的骑行B)