tonsky / datascript

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

Tempid resolution fails in some cases #403

Closed ash14 closed 3 years ago

ash14 commented 3 years ago

Transacting a reference using a tempid to an existing entity fails in some cases.

Using clj -Sdeps '{:deps {datascript/datascript {:mvn/version "1.2.5"}}}':

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

(def conn (d/create-conn {:user/id {:db/unique :db.unique/identity}
                          :message/id {:db/unique :db.unique/value}
                          :message/user {:db/valueType :db.type/ref
                                         :db/cardinality :db.cardinality/one}}))

(d/transact! conn [{:user/id "Alice"}])

; {:tx-data [#datascript/Datom [1 :user/id "Alice" 536870913 true]]
;  :tempids {1 1
;            :db/current-tx 536870913}}

(d/transact! conn [{:db/id "user"
                    :user/id "Alice"}
                   {:message/id 1234
                    :message/user "user"}])

; Execution error (ExceptionInfo) at datascript.db/check-value-tempids (db.cljc:1310).
; Tempids used only as value in transaction: ("user")

Surprisingly, using an alternate transaction form works:

(d/transact! conn [[:db/add "user" :user/id "Alice"]
                   {:message/id 1234
                    :message/user "user"}])

; {:tx-data [#datascript/Datom[2 :message/id 1234 536870914 true]
;            #datascript/Datom[2 :message/user 1 536870914 true]]
;  :tempids {"user" 1
;            2 2
;            :db/current-tx 536870914}}

I'd expect both forms to be equivalent?

tonsky commented 3 years ago

Should be fixed in c3fee52c62c9a873f3361671a737520cf6fe8a32, published as 1.2.6