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

How do I perform a Datomic transaction using the entity id of an enum / ref?

我正在尝试将一个新实体插入到我的 Datomic 数据库中,并引用我创建的枚举/引用类型(:client/gender:client/referral)。

:client/referral 类型的模式条目如下所示(: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]}])

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

我改用 :db/ident 作为引用类型,它解决了问题。

@(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/ident (:gender client)}
             :client/referral      {:db/ident (:referral client)}}])

您输入的数据是错误来源。观察抛出的异常中报的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 徽标受版权保护)