【问题标题】:Django can't flush database during testsDjango 在测试期间无法刷新数据库
【发布时间】:2015-07-25 13:18:34
【问题描述】:

当我尝试运行我的测试(python manage.py test)时,我得到:

CommandError: Database test_db couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: cannot truncate a table referenced in a foreign key constraint
DETAIL:  Table "install_location_2015_05_13" references "app".
HINT:  Truncate table "install_location_2015_05_13" at the same time, or use TRUNCATE ... CASCADE.

我在我们的项目中使用partitions,它是通过 python 函数动态生成的(所以我可以定期运行它)。我没有这些分区的模型。

分区维护函数在syncdb触发post_syncdb信号后调用(所以在建立测试数据库时执行)。

如何让 Django 清除附加表(分区)?要么 如何告诉 Django 在运行测试时使用 CASCADE?

【问题讨论】:

    标签: python django postgresql testing


    【解决方案1】:

    主要问题发生在我们改变 M2M 领域的关系。旧约束仍然存在,并为新关系创建了新约束。 Fixed in Django 1.8

    【讨论】:

    • 我仍然体验到 Django==2.0.2
    • 我也是 Django==3.1.2
    猜你喜欢
    • 2014-12-10
    • 2013-02-14
    • 2019-10-04
    • 2011-02-15
    • 2020-07-14
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多