为什么Datomic在迭代时连续两次产生相同的临时ID? [英] Why does Datomic yield the same temporary ID twice in a row when iterating?

查看:202
本文介绍了为什么Datomic在迭代时连续两次产生相同的临时ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将产生两个不同的id,这是伟大的:

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

(我试过很多想法到目前为止)将产生相同的ID两次,这不是我想要的:

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



全部产生类似

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

其中每个调用产生的数字是相同的。



我实际想要的是不产生数字的调用



任何想法?



只是要清楚,文档说,每次调用tempid都会生成一个唯一的临时ID。



[在@maxthoursie注释之后编辑,重复会在任何情况下出现此问题。

解决方案

使用

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

考虑#...是读取器宏,意味着当读取表达式时它们的值将被解析,这自然只发生一次。仅当您编写文字事务数据(如模式)时,才使用#...宏。使用datomic.api / tempid在运行时生成tempids。


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

解决方案

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.

这篇关于为什么Datomic在迭代时连续两次产生相同的临时ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆