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

Unable to resolve tempids on :db.unique/identity refs #464

Closed aiba closed 4 months ago

aiba commented 4 months ago

When transacting data that contains an attribute that is both :db.unique/identity and :db.type/ref, it appears tempids don't work.

Example:

  (def schema {:user/email {:db/unique :db.unique/identity}
               :profile/user {:db/valueType :db.type/ref
                              :db/cardinality :db.cardinality/one
                              :db/unique :db.unique/identity}})

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

  (datascript/transact! conn
                        [{:db/id "A"
                          :user/email "joe@gmail.com"}
                         {:db/id "B"
                          :profile/avatar "face.jpg"
                          :profile/user "A"}])

This fails with:

Unhandled clojure.lang.ExceptionInfo
   Expected number or lookup ref for entity id, got "A"
   {:entity-id "A", :error :entity-id/syntax}
aiba commented 4 months ago

Upon further reflection, it's pretty weird to have an attribute that is both a :db.type/ref and :db.unique/identity. We changed our schema to {:db/unique :db.unique/value}, and the transactions with tempids are working.

I'll leave this issue open in case there might be a reason to have a ref with unique identity in someone's schema.

tonsky commented 4 months ago

Thanks! Should be fixed