Django在测试期间无法刷新数据库

时间:2015-05-14 03:10:46

标签: python django postgresql testing

当我尝试运行我的测试(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?

1 个答案:

答案 0 :(得分:0)

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