鉴于数据脚本db的这些定义,
(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"))
为什么此查询不返回结果?
对于初学者,您的in子句应为:in $ ?tagname
; 那里的绑定将使您没有默认数据库,这意味着没有任何内容将与您的查询子句匹配。
该$
符号是一个特殊符号,将用作:where
表单中的默认数据库。您可以通过在:where
子句前添加备用数据库的名称符号(例如:in ?alt-db :where [?alt-db ?tag :tag/name ?tagname] ...
)来使用非默认数据库。
我尚未使用过开发卡,因此可能需要做一些其他工作才能使此工作正常进行,但是修复您的查询是第一步。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句