【问题标题】:Django test -- Model object created but cannot be foundDjango 测试——模型对象已创建但找不到
【发布时间】:2018-11-14 19:28:31
【问题描述】:

我正在调用一个名为 People 的模型并执行

People.objects.create(first='foo', last='bar', bio='test')

此模型使用db_table='"people"."background"'

当我运行测试时,执行 People.objects.first() 会找到一些东西,但是执行像 SELECT * from people.background 这样的原始查询却什么也没有。这是为什么呢?

【问题讨论】:

  • 测试在不同的数据库上运行,通常带有 test_ 前缀 iirc。
  • @WillemVanOnsem 我知道这一点。表people.background 已正确创建,检查模型的描述确实表明db_table 应该是people.background。据我所知,它应该连接到正确的数据库,但我找不到创建的数据......

标签: django unit-testing django-models


【解决方案1】:

显然 Django 不正式支持模式。

我想出了一个解决方法,它连接到数据库并直接进行原始查询。本质上,

with connection().cursor as cursor:
    cursor.execute("""INSERT INTO bleh bleh bleh""")  # assuming there's autocommit

编辑:

Django 的回应:Django 不正式支持模式。参见#6148。据我所知,.语法仅适用于 Oracle。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2012-07-28
    相关资源
    最近更新 更多