选择时的Django案例

时间:2018-07-06 08:29:45

标签: django postgresql

我使用原始SQL作为子查询来实现结果。但是,如果我通过带注释的字段进行过滤,则会引发连接到错误(或未声明)别名的错误,如下所示:

django.db.utils.ProgrammingError: invalid reference to FROM-clause entry for table "catalog_good"
HINT:  Perhaps you meant to reference the table alias "v0".

我想知道是否有机会通过Django进行CASE-WHEN-SELECT。 这是我想通过Django类改进的代码的一部分:

CASE
    WHEN (
        SELECT (score - "catalog_tag"."weight") ^ 2
        FROM "catalog_tag"
        WHERE "catalog_tag"."word" = 'chair' AND "catalog_tag"."good_id" = "catalog_good"."id"
    ) IS NOT NULL THEN (
        SELECT (score - "catalog_tag"."weight") ^ 2
        FROM "catalog_tag"
        WHERE "catalog_tag"."word" = 'chair' AND "catalog_tag"."good_id" = "catalog_good"."id"
    )
    ELSE (score ^ 2)
END

谢谢。

0 个答案:

没有答案