2016-01-23 66 views
1

我無法從我在Korma中映射的實體中選擇SELECT COUNT(*)無法爲默認字段的Korma實體選擇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包含我的實體字段。我如何覆蓋它?

+0

http://stackoverflow.com/questions/25825252/restricting-select-fields-with-korma是模糊相似,但涉及「(select)」的默認行爲,沒有指定字段。 – fzzfzzfzz

回答

1

你不能重寫它本身。 Korma查詢操作函數爲always additive,所以指定字段僅指定附加的字段。

要解決這個問題,你可以rewrite this query to select against the users table itself而非科爾馬實體users

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

但隨後你將不得不湊合沒有在users實體其他定義的東西。加入with

(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])))``` 

然後,你可以寫你的正常查詢:

或者,你可以改寫這個實體沒有定義任何實體字段,然後定義這個實體與所需的默認字段包裝版本/ where條款,該「用戶」查詢,只有直接接觸users-raw當你需要排除這些領域:

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