为什么这个查询没有返回结果?

3

鉴于这些 datascript 数据库的定义,

(def schema
  {:tag/name { :db/unique :db.unique/identity }
   :item/tag {:db/valueType   :db.type/ref
               :db/cardinality :db.cardinality/many}
   :outfit/item {:db/valueType   :db.type/ref
                 :db/cardinality :db.cardinality/many}}
)
(defonce conn (d/create-conn schema))

(defn new-entity! [conn attrs]
  (let [entity (merge attrs {:db/id -1})
        txn-result (d/transact! conn [entity])
        temp-ids (:tempids txn-result)]
    (temp-ids -1)))

(defonce init
  (let [tag1    (new-entity! conn {:tag/name "tag1"})
        item1   (new-entity! conn {:item/tag tag1})
        outfit1   (new-entity! conn {:outfit/item item1})]
    :ok))

如果我运行这个开发卡,就不会得到任何结果:
(defcard find-by-tag-param
  "find items by tag"
  (d/q '[ :find ?item 
         :in ? ?tagname
         :where
         [ ?tag :tag/name ?tagname ]
         [ ?item :item/tag ?tag ]]
       @conn "tag1"))

为什么这个查询没有返回结果?
1个回答

3
首先,你的in子句应该是:in $ ?tagname;你在那里绑定的内容没有默认数据库,这意味着没有匹配查询子句。
符号$是一个特殊符号,它在:where表单中用作默认数据库。您可以通过使用替代db的名称符号作为前缀来使用非默认数据库(例如::in ?alt-db :where [?alt-db ?tag :tag/name ?tagname] ...)。
我没有使用过dev cards,所以可能需要其他操作才能使其正常工作,但是修复查询是第一步。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接