我有以下模型(campaigns/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)并产生测试结果(test run)。
- 对于每次活动运行,从测试表中收集测试相关信息
显然,存在循环关系,数据库的完整性仅由应用程序来保证。
但是,从数据库架构师的角度来看,这种循环关系是不正确的,因为它会导致完整性问题:
- 在 SQL 中直接或从 django 管理模块中,可以在campaign_testrun 中创建不在campaign_tests 表中的测试
由于上述原因,解决方案是直接将campaigns_testrun 与campaigns_campaign_tests 链接起来。但是,我认为这在 Django 中是不可能的。
如果您有任何想法如何处理此问题,欢迎发表评论。
预先感谢您的帮助。