我无法从我在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包含了我的实体字段,尽管我指定了要在这个查询中选择的字段。我怎么才能推翻它?
发布于 2016-01-23 18:57:34
你不能推翻它本身。Korma查询操作函数是总加性,因此指定字段只是指定附加字段。
为了解决这个问题,您可以使用表本身而不是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))https://stackoverflow.com/questions/34967470
复制相似问题