Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

clojure - Why does Datomic yield the same temporary ID twice in a row when iterating?

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.]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...