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

Upsert on entity with identity composite tuple #473

Closed handerpeder closed 2 months ago

handerpeder commented 3 months ago

Though this was the same as #323 but maybe not.

Trying to upsert on an entity that has a composite primary key, I would expect this to work:

(def schema {:a+b {:db/tupleAttrs [:a :b]
                   :db/unique :db.unique/identity}})
(def conn (d/create-conn schema))
(d/transact! conn [{:a 1 :b 2}])
(d/transact! conn [{:a 1 :b 2}])

The second invocation throws

1. Unhandled clojure.lang.ExceptionInfo
   Cannot add #datascript/Datom [2 :a+b [1 2] 536870914 true] because
   of unique constraint: (#datascript/Datom [1 :a+b [1 2] 536870913
   true])
   {:error :transact/unique,
    :attribute :a+b,
    :datom #datascript/Datom [2 :a+b [1 2] 536870914 true]}

Since these two attributes are marked as identity, this should be an update on 1, not a new entity.

tonsky commented 2 months ago

Fixed and pushed as 1.7.0