运行 测试 Django Pycharm 时使用了错误的 table

Wrong table used when running tests Django Pycharm

我使用 inspectdb 从与 MySQL Connector/Python Connector/Python 连接的 MySQL 数据库导入模型模式。

当我运行测试时,系统显示:

django.db.utils.ProgrammingError: Table 'test_mydb.cards' doesn't exist

但是 table 名字只是 mydb.cards,而不是 test_mydb.cards

为什么要添加这个前缀?我的应用名称只是 container.

Django 使用 unittest 模块进行测试,它还创建一个空白数据库进行测试,因为测试应该 运行 理想情况下总是在同一个空白数据库或 fixtures 填充的数据库上。

Tests that require a database (namely, model tests) will not use your “real” (production) database. Separate, blank databases are created for the tests.

Regardless of whether the tests pass or fail, the test databases are destroyed when all the tests have been executed.

检查您是否为 table 测试尝试访问进行了迁移排序,因为所有 table 没有迁移的测试套件都无法在没有 monkeypatching 或变通方法的情况下访问

我找到了原因:我在 models.py Class Meta 中有 managed 属性 到 False 每个 table。我从来没有自己设置它,它是在 inspectdb.

之后的默认设置

请阅读:Django Models Options: Managed