gpt4 book ai didi

python - django.db.utils.IntegrityError : FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django 错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:16 26 4
gpt4 key购买 nike

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

  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

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

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

环境细节

  • Django 2.1 版
  • SQLite3 版本 3.20.0

最佳答案

关于 Django versionDatabase typeversion 的更多信息连同您的代码试验 会帮助我们以更好的方式调试此问题。

但是,这个错误信息...

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 release notes :

  • > Foreign key constraints are now enabled on SQLite :如果尝试保存违反外键约束的现有模型实例,这是一个向后不兼容的更改(IntegrityError:外键约束失败)。
  • 外键现在使用 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 在这个discussion举例如下:

    • 在您的代码中寻找类似这样的模式:

       # 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 约束 错误 而失败,因为 PageBlock 必须有部分,因此您不能在不先分配它的情况下调用创建。

关于python - django.db.utils.IntegrityError : FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com