Question

This will produce two different ids, which is great:

#db/id[:db.part/user]
#db/id[:db.part/user]

but anything like the following (I tried a lot of ideas so far) will produce the same id twice, which is not what I want:

(repeatedly 2 (fn [] #db/id[:db.part/user]))
(for [n [1 2]] #db/id[:db.part/user])

All yield something like

(#db/id[:db.part/user -1000774] #db/id[:db.part/user -1000774])

where the number produced is the same for each call.

What I actually want is for the calls to NOT produce a number at all, so that I can just add the produced data via a transaction.

Any ideas?

Just to be clear, the documentation says, "Each call to tempid produces a unique temporary id."

[Edited after comment by @maxthoursie that repeat would be having this problem in any case.]

Was it helpful?

Solution

Use

(require '[datomic.api :as d])
(repeatedly 2 #(d/tempid :db.part/user))
;; => (#db/id[:db.part/user -1000118] #db/id[:db.part/user -1000119])

Consider that #... are reader macros meaning that their value will be resolved when the expression is read which naturally happens only once. Use the #... macro only when you are writing literal transaction data (like a schema). Use datomic.api/tempid to generate tempids in runtime.

OTHER TIPS

Because repeat is repeating the value it got from calling id once.

Use repeatedly instead.

See examples at http://clojuredocs.org/clojure_core/clojure.core/repeatedly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top