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

时间:2015-10-12 01:08:34

标签: clojure enums ref datomic

我尝试将新实体插入我的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]}])

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

2 个答案:

答案 0 :(得分:3)

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

  

[#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徽标,我相信受版权保护)

答案 1 :(得分:-1)

我转而使用: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)}}])