2016-04-29 64 views
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)) 

如果我运行这个devcard,我没有得到任何结果:

(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")) 

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

回答

3

对于初学者,你的子句应该是:in $ ?tagname;你在那里的绑定没有默认的数据库,这意味着什么都不会匹​​配你的查询子句。

$符号是一个特殊符号,它被用作:where表单中的默认数据库。您可以使用非默认数据库,将:where子句加上备用数据库的名称符号(例如:in ?alt-db :where [?alt-db ?tag :tag/name ?tagname] ...)。

我还没有使用dev卡,所以有可能还有其他的东西需要这个工作,但修复你的查询是第一步。

相关问题