2016-04-29 4 views
3

datascript DBのこれらの定義、このクエリで結果が返されないのはなぜですか?私はこのdevcardを実行する場合

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

なぜこのクエリは結果を返しませんか?

答えて

3

まず、in節は:in $ ?tagnameである必要があります。そこにあるバインディングによって、既定のデータベースがなくなります。つまり、クエリ句に一致するものはありません。

$シンボルは、:whereフォームのデフォルトデータベースとして使用される特別なシンボルです。 :where句の先頭に代替データベースの名前記号(例::in ?alt-db :where [?alt-db ?tag :tag/name ?tagname] ...)を付けることで、デフォルト以外のデータベースを使用できます。

私はdevカードで作業していないので、これを行うには何か他のものが必要かもしれませんが、クエリを修正することが最初のステップです。

関連する問題