Skip to content
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

Lookup ref in composite tuple during upsert #452

Open
telekid opened this issue Jul 24, 2023 · 1 comment
Open

Lookup ref in composite tuple during upsert #452

telekid opened this issue Jul 24, 2023 · 1 comment

Comments

@telekid
Copy link

telekid commented Jul 24, 2023

Back with another weird one. In the following example, I'd like to upsert an entity via composite tuple. One element of the composite tuple is a ref. At insertion time, I only know the natural identity (:db/uuid) of said referent, not its datascript :db/id.

It occurred to me that I may be able to supply a lookup ref as an element within a composite tuple that is being used for upsert, but it appears that is not the case.

Does this seem like a useful feature? Any suggested workarounds for its absence (that don't require multiple transactions?)

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

(def schema
  {:db/uuid {:db/unique    :db.unique/identity}
   :user/db {:db/valueType :db.type/ref}
   :user/dbid {}
   :user/db+dbid {:db/valueType :db.type/tuple
                  :db/unique :db.unique/identity
                  :db/tupleAttrs [:user/db :user/dbid]}})

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

(d/transact!
 db
 [{:db/uuid "A"}
  {:user/db [:db/uuid "A"]
   :user/dbid 1}])

;; This transaction will fail
(d/transact!
 db            ;; |    BELOW   |
 [{:user/db+dbid [[:db/uuid "A"] 1] ;; This will work if you change `[:db/uuid "A"]` to `1`
   :user/name "jake"}])

;; Execution error (ClassCastException) at datascript.db/value-compare (db.cljc:387).
;; class clojure.lang.PersistentVector cannot be cast to class java.lang.Number
@tonsky
Copy link
Owner

tonsky commented Jul 30, 2023

Yes, seems useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants