Clojure 表的子句条目中缺少?

Clojure 表的子句条目中缺少?,clojure,sqlkorma,Clojure,Sqlkorma,当我从has many关系中的表中选择一个特定字段时,会出现如下异常:org.postgresql.util.PSQLException:ERROR:表\“bar\”的子句条目中缺少 以下是我定义的关系: (declare foo bar) (korma/defentity foo (korma/pk :token) (korma/database db) (korma/table :foo)

当我从has many关系中的表中选择一个特定字段时,会出现如下异常:
org.postgresql.util.PSQLException:ERROR:表\“bar\”的子句条目中缺少

以下是我定义的关系:

(declare foo bar)

(korma/defentity foo
                 (korma/pk :token)
                 (korma/database db)
                 (korma/table :foo)
                 (korma/has-many bar {:fk :token}))

(korma/defentity bar
                 (korma/pk :token)
                 (korma/database db)
                 (korma/table :bar)
                 (korma/belongs-to foo {:fk :token}))
以下是我在sqlkorma中生成的查询:

(defn fetch []
  (->
    (korma/select* foo)
    (korma/with bar)
    (korma/fields :foo.token :bar.col)
    (korma/exec)))

如果我选择了所有使用with
*
的字段,那么查询运行时不会出现问题。但是,我想指定要选择哪些字段,我做错了什么?

我放弃了使用Korma进行选择。我认为使用基本SQL互操作可能会更好:

(ns tst.demo.jdbc
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [clojure.java.jdbc :as jdbc]
    [clojure.string :as str]
    [clojure.java.io :as io] ))

(def db {:classname   "org.h2.Driver"
         :subprotocol "h2:mem"
        ;:subname     "./korma.db"
         :subname     "demo;DB_CLOSE_DELAY=-1" ; -1 very important!!!
                          ; http://www.h2database.com/html/features.html#in_memory_databases
                          ; http://makble.com/using-h2-in-memory-database-in-clojure
         :user "sa"
         :password ""
         })

(dotest
  (spyx (jdbc/db-do-commands db ["drop table if exists langs"]))
  (spyx (jdbc/db-do-commands db ["drop table if exists releases"]))
  (spy :create
       (jdbc/db-do-commands
         db
         (jdbc/create-table-ddl :langs
                                [[:id :serial]
                                 [:lang "varchar not null"]])))
  (spy :create
       (jdbc/db-do-commands
         db
         (jdbc/create-table-ddl :releases
                                [[:id :serial]
                                 [:desc "varchar not null"]
                                 [:langId "numeric"]])))
  (spy :insert
       (jdbc/insert-multi! db :langs
                           [{:lang "Clojure"}
                            {:lang "Java"}]))
  ;; -> ({:lang "Clojure", :id 1} {:lang "Java", :id 2})
  (spyx-pretty (jdbc/query db ["select * from langs"]))

  (let [clj-id (grab :id (only (jdbc/query db ["select id from langs where lang='Clojure'"])))]
    (spyx clj-id)
    (spy :insert-rel
         (jdbc/insert-multi! db :releases
                             [{:desc "ancients" :langId clj-id}
                              {:desc "1.8" :langId clj-id}
                              {:desc "1.9" :langId clj-id}])) )
  (let [java-id (grab :id (only (jdbc/query db ["select id from langs where lang='Java'"])))]
    (spyx java-id)
    (spy :insert-rel
         (jdbc/insert-multi! db :releases
                             [{:desc "dusty" :langId java-id}
                              {:desc "8" :langId java-id}
                              {:desc "9" :langId java-id}
                              {:desc "10" :langId java-id}])) )
  (spyx-pretty
   (jdbc/query db [
   "select langs.lang, releases.desc
       from langs inner join releases
       on (langs.id = releases.langId)
       where lang = 'Clojure' "]) )
  )
结果:

(jdbc/db-do-commands db ["drop table if exists langs"]) => (0)
(jdbc/db-do-commands db ["drop table if exists releases"]) => (0)
:create => (0)
:create => (0)
:insert => ({:id 1} {:id 2})
(jdbc/query db ["select * from langs"]) =>
({:id 1, :lang "Clojure"} {:id 2, :lang "Java"})
clj-id => 1
:insert-rel => ({:id 1} {:id 2} {:id 3})
java-id => 2
:insert-rel => ({:id 4} {:id 5} {:id 6} {:id 7})
(jdbc/query db ["select langs.lang, releases.desc\n       from langs inner join releases\n       on (langs.id = releases.langId)\n       where lang = 'Clojure' "]) =>
({:lang "Clojure", :desc "ancients"}
 {:lang "Clojure", :desc "1.8"}
 {:lang "Clojure", :desc "1.9"})

想想看,我在错误的位置传递了
bar.col
字段。我想因为这是一个有很多关系的问题,所以SQLKorma必须离开并在这里运行n+1查询。这解释了为什么我在子句异常中看到一个
缺失,因为我告诉sqlkorma在错误的查询中获取一个字段

(defn fetch []
  (->
    (korma/select* foo)
    (korma/fields :token)
    (korma/with bar (korma/fields :col))
    (korma/exec)))

这似乎不能解决我的问题。你应该试试Walkable。它是Clojure的一个新sql库,使用了一种非常有表现力的查询语言