简体   繁体   English

定义多个模型之间的关系 - Django

[英]Defining relationships across Multiple Models - Django

I have the following relationship between three Django models: 我在三个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

Please ignore TestSuite : it's not important for this problem. 请忽略TestSuite :这对于这个问题并不重要。 Every TestCase is what you may imagine: a TestCase (class). 每个TestCase都是您可能想象的: TestCase (类)。 So every time one executes a TestCase it has a TestExecution and a Result : 因此,每次执行一个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

That can be said, a TestCase hasMany Result but the relationship between Result and TestExecution is OneToOne . 可以hasMany说, TestCase ResultResultTestExecution之间的关系是OneToOne I'm having some problems with my models schema. 我的模型架构遇到了一些问题。 I know a simple solution would be to merge the tables Result and TestExecution : 我知道一个简单的解决方案是mergeResultTestExecution

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

What's the most appropriate way to proceed regarding the models schema? 关于模型架构,最合适的方法是什么?

Adding the autotester/migrations/0005_auto_20150519_1831.py as per request: 根据请求添加autotester/migrations/0005_auto_20150519_1831.py

# - - coding: utf-8 - - from future import unicode_literals # - - coding:utf-8 - 来自未来的导入unicode_literals

from django.db import models, migrations 来自django.db导入模型,迁移

class Migration(migrations.Migration): class Migration(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,
       ),
   ]           

A OneToOneField is a like a ForeignKey with unique=True . OneToOneField类似于ForeignKey ,其unique=True

You problem comes with this unique constraint since you can't add a field with a unique constraint if your database is not empty. 如果您的数据库不为空,则无法添加具有唯一约束的字段,因此您会遇到此问题。 What you have to do is: 你要做的是:

  1. add the field without a unique constraint = ForeignKey 添加没有唯一约束的字段= ForeignKey
  2. Fill this field regarding the upcoming unique constraint you will have 填写此字段,了解您将拥有的即将到来的唯一约束
  3. Change the ForeignKey to a OneToOneField 将ForeignKey更改为OneToOneField

Here are the detailed steps: 以下是详细步骤:

First step: 第一步:

Delete your autotester/migrations/0005_auto_20150519_1831.py file and change your result field with result = models.ForeignKey(Result, null=True, blank=True) in your TestExecution model and do the migration: 删除autotester/migrations/0005_auto_20150519_1831.py文件,并在TestExecution模型中使用result = models.ForeignKey(Result, null=True, blank=True)更改result字段并执行迁移:

./manage.py makemigrations autotester
./manage.py migrate autotester

Second Step: 第二步:

For every Result you have, create a TestExecution (replace FOO with the appropriate datas): 对于您拥有的每个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()

Third step 第三步

Change your result field with result = models.OneToOneField(Result) and then do the migration: 使用result = models.OneToOneField(Result)更改结果字段,然后执行迁移:

./manage.py makemigrations autotester
./manage.py migrate autotester

You should be good to go. 你应该好好去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM