4

我可以成功运行所有单元测试,如果我运行独立服务器,我什至可以成功运行 selenium 测试,但是当我尝试使用 LiveServerTestCases 以独立的方式测试所有内容时,每个 LiveServerTestCase 测试在完成后以以下错误结束拆解功能:

  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\test\testcases.py", line 209, in __call__
    self._post_teardown()
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\test\testcases.py", line 908, in _post_teardown
    self._fixture_teardown()
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\test\testcases.py", line 943, in _fixture_teardown
    inhibit_post_migrate=inhibit_post_migrate)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\core\management\__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\core\management\commands\flush.py", line 80, in handle
    emit_post_migrate_signal(verbosity, interactive, database)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\core\management\sql.py", line 51, in emit_post_migrate_signal
    **kwargs
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\dispatch\dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\dispatch\dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\contrib\auth\management\__init__.py", line 79, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\models\query.py", line 471, in bulk_create
    obj_without_pk._state.db = self.db
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\transaction.py", line 212, in __exit__
    connection.commit()
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 261, in commit
    self._commit()
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit
    return self.connection.commit()
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

我很确定我的数据库模型中没有任何错误,因为当我启动一个单独的服务器实例并行运行时,所有单元测试都运行良好,硒测试运行良好,所以我猜它有与硒有关吗?

我试过使用 Chrome webdriver、IE webdriver 和 Firefox webdriver。结果相同。它似乎与我的数据库无关,因为该错误仅发生在 LiveServerTestCases 上。

环境细节

  • Django 2.1 版
  • SQLite3 版本 3.20.0
4

1 回答 1

3

有关Django 版本数据库 类型版本的更多信息以及您的代码试验将有助于我们以更好的方式调试此问题。

但是,此错误消息...

File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\core\management\sql.py", line 51, in emit_post_migrate_signal **kwargs
.
File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

...意味着在尝试保存现有模型实例时引发了IntegrityError 。

根据Django 2.0 发行说明

  • 现在在 SQLite 上启用了外键约束:如果尝试保存违反外键约束的现有模型实例,这是一个向后不兼容的更改(IntegrityError: FOREIGN KEY constraint failed )。
  • 现在使用DEFERRABLE INITIALLY DEFERRED而不是DEFERRABLE IMMEDIATE创建外键。因此,可能需要重建表以使用新定义重新创建外键,特别是如果您使用如下模式;

    from django.db import transaction
    
    with transaction.atomic():
        Book.objects.create(author_id=1)
        Author.objects.create(id=1)
    
  • 如果您不将外键重新创建为DEFERRED,则第一个create()将失败,因为强制执行外键约束。

  • @dirkgroten 在此讨论中提供了一个示例,如下所示:

    • 在你的代码中寻找这样的模式:

       # in pagetree/models.py, line 810
       @classmethod
       def create_from_dict(cls, d):
          return cls.objects.create()  # what happens to d by the way?
      
    • 这肯定会因ForeignKey constraint 错误而失败,因为 PageBlock 必须有部分,所以如果没有先分配它就不能调用 create。

于 2019-01-21T09:08:48.810 回答