gpt4 book ai didi

clojure - 如何使用枚举/引用的实体 id 执行 Datomic 事务?

转载 作者:行者123 更新时间:2023-12-01 03:37:50 24 4
gpt4 key购买 nike

我正在尝试将一个新实体插入到我的 Datomic 数据库中,并引用我创建的 enum/ref 类型( :client/gender:client/referral )。
:client/referral 的架构条目type 看起来像这样(并且 :client/gender 定义几乎相同):

{:db/id #db/id[:db.part/db]
:db/ident :client/referral
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The referral source for this client"
:db.install/_attribute :db.part/db
}

[:db/add #db/id[:db.part/user] :db/ident :client.referral/friend]
[:db/add #db/id[:db.part/user] :db/ident :client.referral/online]
[:db/add #db/id[:db.part/user] :db/ident :client.referral/radio]

事务函数看起来像这样(我已经尝试了各种变体):
(defn add-client [client]
(let [gender (:gender client)
referral (:referral client)]
@(d/transact conn
[{:db/id (d/tempid :db.part/user)
:client/name (:name client)
:client/phone (:phone client)
:client/email (:email client)
:client/date-of-birth (:dateOfBirth client)
:client/gender gender
:client/referral referral}])))

这种特殊的变化会产生以下错误:
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: :db.error/not-an-entity Unable to resolve entity: 17592186045422 in datom [#db/id[:db.part/user -1000022] :client/gender "17592186045422"]
...
Caused by: datomic.impl.Exceptions$IllegalArgumentExceptionInfo: :db.error/not-an-entity Unable to resolve entity: 17592186045422 in datom [#db/id[:db.part/user -1000022] :client/gender "17592186045422"]
error.clj:57 datomic.error/arg
error.clj:55 datomic.error/arg
db.clj:555 datomic.db/require-id
db.clj:2334 datomic.db/datomic.db.ProcessInpoint
db.clj:2317 datomic.db/datomic.db.ProcessInpoint
db.clj:2512 datomic.db/with-tx[fn]
PersistentVector.java:333 clojure.lang.PersistentVector.reduce
core.clj:6518 clojure.core/reduce
db.clj:2512 datomic.db/with-tx[fn]
db.clj:2516 datomic.db/with-tx
peer.clj:558 datomic.peer.LocalConnection/fn
peer.clj:558 datomic.peer/datomic.peer.LocalConnection
peer.clj:550 datomic.peer/datomic.peer.LocalConnection
api.clj:94 datomic.api/transact
...

我也尝试过这样的变体(除其他外)但无济于事:
@(d/transact conn
[{:db/id (d/tempid :db.part/user)
:client/name (:name client)
:client/phone (:phone client)
:client/email (:email client)
:client/date-of-birth (:dateOfBirth client)
:client/gender #db/id[:db.user/part gender]
:client/referral #db/id[:db.user/part referral]}])

什么,可能很明显,我错过了什么?

最佳答案

您的输入数据是错误源。观察抛出的异常中报告的datom的值位置中的字符串。

[#db/id[:db.part/user -1000022] :client/gender "17592186045422"]



它应该是性别实体的实体 ID 或通过 :db/ident 识别它的关键字。

确保您的交易内容如下:
[:db/add (d/tempid :db.part/user) :client/gender :gender/male]

(假设您有一个带有 :db/ident :gender/male 的实体)

(旁注:你的头像,Clojure 标志,我相信是受版权保护的)

关于clojure - 如何使用枚举/引用的实体 id 执行 Datomic 事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com