跨多个模型定义关系 - Django
Posted
技术标签:
【中文标题】跨多个模型定义关系 - Django【英文标题】:Defining relationships across Multiple Models - Django 【发布时间】:2015-07-31 18:09:31 【问题描述】:我在三个Django
模型之间有如下关系:
class TestCase(models.Model):
'''
Define the testcase model. A testcase is a python Class
which contains a set of tests
'''
name = models.BinaryField(blank=False)
filename = models.BinaryField(blank=True)
run_flag = models.IntegerField(default=0)
run_as_root = models.BooleanField(default=0)
num_tests = models.IntegerField(default=0)
testsuite = models.ForeignKey(TestSuite)
def __str__(self):
return self.name
请忽略TestSuite
:这对这个问题并不重要。每个TestCase
都是你想象的:一个TestCase
(类)。所以每次执行TestCase
时都会有TestExecution
和Result
:
class Result(models.Model):
'''
Define the result of a testcase. It may be 'PASS', 'FAIL',
'SKIPPED' or 'ABORTED'
'''
FAIL = 0
PASS = 1
ABORTED = 2
SKIPPED = 3
Status = (
(PASS, 'PASS'),
(FAIL, 'FAIL'),
(SKIPPED, 'SKIPPED'),
(ABORTED, 'ABORTED'),
)
status = models.IntegerField(choices=Status, default=FAIL)
testcase = models.ForeignKey(TestCase)
class TestExecution(models.Model):
name = models.BinaryField(blank=False)
num_testsuites = models.IntegerField(default=0)
time = models.FloatField()
date = models.DateTimeField(default=django.utils.timezone.now)
result = models.OneToOneField(Result)
def __str__(self):
return self.name + " : " + self.date + " : " + self.time
可以说是TestCase
hasMany
Result
,但Result
和TestExecution
的关系是OneToOne
。我的模型架构有一些问题。我知道一个简单的解决方案是merge
表Result
和TestExecution
:
python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, autotester, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying autotester.0005_auto_20150519_1831...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 403, in add_field
self.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1062, "Duplicate entry '0' for key 'result_id'")
关于模型架构的最合适的处理方式是什么?
根据要求添加autotester/migrations/0005_auto_20150519_1831.py
:
# -- 编码:utf-8 -- 从未来导入 unicode_literals
从 django.db 导入模型,迁移
类迁移(migrations.Migration):
dependencies = [
('autotester', '0004_auto_20150519_1744'),
]
operations = [
migrations.RemoveField(
model_name='testexecution',
name='framework',
),
migrations.AddField(
model_name='testexecution',
name='result',
field=models.OneToOneField(default=None, to='autotester.Result'),
preserve_default=False,
),
]
【问题讨论】:
您的迁移有问题吗? 你能给我们看看 autotester.0005_auto_20150519_1831 吗? 【参考方案1】:OneToOneField
类似于 ForeignKey
和 unique=True
。
您的问题与此唯一约束有关,因为如果您的数据库不为空,则无法添加具有唯一约束的字段。你要做的是:
-
添加没有唯一约束的字段 =
ForeignKey
填写此字段,了解即将到来的唯一约束
将 ForeignKey 更改为 OneToOneField
详细步骤如下:
第一步:
删除您的 autotester/migrations/0005_auto_20150519_1831.py
文件并在您的 result = models.ForeignKey(Result, null=True, blank=True)
模型中将您的 result
字段更改为 TestExecution
并进行迁移:
./manage.py makemigrations autotester
./manage.py migrate autotester
第二步:
对于您拥有的每个Result
,创建一个TestExecution
(将 FOO 替换为适当的数据):
results = Result.objects.all()
for result in results:
tst = TestExecution()
tst.name = FOO
tst. num_testsuites = FOO
tst.time = FOO
tst.result_id = result.id
tst.save()
第三步
使用result = models.OneToOneField(Result)
更改您的结果字段,然后进行迁移:
./manage.py makemigrations autotester
./manage.py migrate autotester
你应该很高兴。
【讨论】:
删除result = models.OneToOneField(Result)
?
迁移成功了……我现在正在解析我的 xml 测试结果!完成后我会在那里发帖
我认为它不起作用...Unknown column 'autotester_testexecution.result_id' in 'where clause'
以上是关于跨多个模型定义关系 - Django的主要内容,如果未能解决你的问题,请参考以下文章