在数据库中获取插入实体的ID? [英] Getting the id of an inserted entity in datomic?
问题描述
在我在数据集中运行事务以插入值后,我如何使用事务的返回值来获取创建的任何实体的ID?
After I run a transaction in datomic to insert a value, how I can use the return value of the transaction to get the ids of any entities that were created?
这里是插入后的返回值的示例:
Here is a sample of the return value I get after an insert:
#<promise$settable_future$reify__4841@7c92b2e9: {:db-before datomic.db.Db@62d0401f, :db-after datomic.db.Db@bba61dfc,
:tx-data [#Datum{:e 13194139534331 :a 50
:v #inst "2013-06-19T11:38:08.025-00:00"
:tx 13194139534331 :added true} #Datum{:e 17592186045436 .....
我可以看到基础数据...如何提取它们的值?
I can see the underlying datums...how can I extract their values?
推荐答案
使用 d / resolve-tempid 。如果你要处理一个单一的实体,看看:tx-data
可以工作,但如果你的事务包含多个实体,那么你不知道它们出现在:tx-data
中。
Use d/resolve-tempid. If you were to transact a single entity, looking at :tx-data
would work but if your transaction contained more than one entity, then you wouldn't know the order in which they appear in :tx-data
.
您应该做的是使用(d / tempid)
向您的实体提供临时ID >或其字面表示#db / id [:db.part / user _negativeId _]
,然后使用 d / resolve-tempid
从您的临时ID转到数据库给出的真实ID。代码看起来像:
What you should do is give temporary ids to your entities (before transacting them) using either (d/tempid)
or its literal representation #db/id[:db.part/user _negativeId_]
and then use d/resolve-tempid
to go from your temporary id to the real id given by the database. The code would look something like:
(d/resolve-tempid (d/db conn) (:tempids tx) (d/tempid :db.part/user _negativeId_))
strong>,请参阅此 gist 。
For a full code sample, see this gist.
这篇关于在数据库中获取插入实体的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!