【问题标题】:Unable to SELECT COUNT(*) for Korma entity with default fields无法为具有默认字段的 Korma 实体选择 COUNT(*)
【发布时间】:2023-03-25 22:45:01
【问题描述】:

我无法通过我在 Korma 中映射的实体SELECT COUNT(*)

这是我的实体:

(declare users responses) (korma/defentity users (korma/entity-fields :id :slack_id :active :token :token_created) (korma/many-to-many responses :userresponses))

这是我对SELECT COUNT(*)的尝试:

(korma/select schema/users (korma/fields ["count(*)"]) (korma/where {:slack_id slack-id}))

我收到此错误:

ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function at character 8 STATEMENT: SELECT "users"."id", "users"."slack_id", "users"."active", "users"."token", "users"."token_created", count(*) FROM "users" WHERE ("users"."slack_id" = $1)

看起来 Korma 包含了我的实体字段,即使我在此查询中指定了要选择的字段。我该如何覆盖它?

【问题讨论】:

标签: clojure korma


【解决方案1】:

您本身无法覆盖它。 Korma 查询操作函数是always additive,因此指定字段仅指定附加 字段。

要解决这个问题,您可以rewrite this query to select against the users table itself 而不是 Korma 实体 users

(korma/select :users
  (korma/fields ["count(*)"])
  (korma/where {:slack_id slack-id}))

但是,您将不得不在没有在 users 实体中定义的任何其他内容的情况下进行操作。

或者,您可以重写此实体以不定义任何实体字段,然后使用所需的默认字段定义此实体的包装版本:

(korma/defentity users-raw
  (korma/many-to-many responses :userresponses)))

(def users
  (korma/select
    users-raw
    (korma/fields [:id :slack_id :active :token :token_created])))```

然后您可以通过在此“用户”查询中添加with/where 子句来编写您的普通查询,并且仅在需要排除这些字段时直接触摸users-raw

(-> users (with ...) (where ...) (select))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多