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

走远了吗. 提交于 2019-12-18 09:16:00

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/20015996/why-does-datomic-yield-the-same-temporary-id-twice-in-a-row-when-iterating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!