tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

Transacting many ref value as set of inline maps #476

Closed aiba closed 2 months ago

aiba commented 2 months ago

This worked in 1.6.5:

(require '[datascript.core :as d])

(def schema {:item/tags {:db/valueType :db.type/ref
                         :db/cardinality :db.cardinality/many}})

(def conn (d/create-conn schema))

(d/transact! conn [{:db/id "1"
                    :item/tags #{{:tag/name "foo"}
                                 {:tag/name "bar"}}}])

In 1.7.1, it throws an error, "Expected number or lookup ref for entity id, got nil".

I tracked it to the new function, assoc-auto-tempids, which only assigns tempids to inline maps of a many ref value if the value is sequential?, which sets are not. So now one must use a sequential value, such as:

(d/transact! conn [{:db/id "1"
                    ;; Vector, not set.
                    :item/tags [{:tag/name "foo"}
                                {:tag/name "bar"}]}])

But I think it's logical to be able to transact many ref entities inline as a set of maps, since that's also how they are returned when queried.

Is this as simple as changing This line to check coll? rather than sequential??

tonsky commented 2 months ago

Yeah sorry about that. Try 1.7.2

aiba commented 2 months ago

Working great in 1.7.2, thank you so much!