Skip to content

Fix upserts containing tempids in tuples #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix upserts containing tempids in tuples
  • Loading branch information
Ash committed Dec 18, 2020
commit 860549be1c59befc1da8dc8d342791356090c63a
9 changes: 6 additions & 3 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1100,10 +1100,13 @@
:email {\"ivan@\" 2}
:alias {\"abc\" 3
\"def\" 4}}}"
[db entity]
[db entity tempids]
(if-some [idents (not-empty (-attrs-by db :db.unique/identity))]
(let [resolve (fn [a v]
(:e (first (-datoms db :avet [a v]))))
(let [v (if (coll? v)
(mapv #(get tempids % %) v)
v)]
(:e (first (-datoms db :avet [a v])))))
split (fn [a vs]
(reduce
(fn [acc v]
Expand Down Expand Up @@ -1350,7 +1353,7 @@
(cons (assoc entity :db/id id) entities)))

;; upserted => explode | error
:let [[entity' upserts] (resolve-upserts db entity)
:let [[entity' upserts] (resolve-upserts db entity tempids)
upserted-eid (validate-upserts entity' upserts)]

(some? upserted-eid)
Expand Down
23 changes: 22 additions & 1 deletion test/datascript/test/tuples.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,28 @@
[2 :b "b"]
[2 :a+b ["a" "b"]]
[2 :c "c"]}
(tdc/all-datoms (d/db conn))))))
(tdc/all-datoms (d/db conn)))))

(testing "#378"
(let [conn (d/create-conn {:player {:db/unique :db.unique/identity}
:home {:db/valueType :db.type/ref}
:away {:db/valueType :db.type/ref}
:players {:db/unique :db.unique/identity
:db/tupleAttrs [:home :away]}})]
(d/transact! conn [[:db/add -1 :player "Nadal"]
[:db/add -2 :player "Federer"]
{:home -1
:away -2}])
(d/transact! conn [{:db/id "p1"
:player "Nadal"}
{:db/id "p2"
:player "Federer"}
{:db/id "match"
:players ["p1" "p2"]
:game 3}])

(is (= 3 (:game (d/entity @conn 3)))
"Upsert successful"))))

(deftest test-lookup-refs
(let [conn (d/create-conn {:a+b {:db/tupleAttrs [:a :b]
Expand Down