以下使用简化的原子设置:
:account/user -> string
:account/email -> ref
:email/name -> string
:email/type -> keyword
如果我有包含帐户信息的实体,很容易知道它有电子邮件信息
(keys <account entity>)
;; => [:account/user :account/email]
(:account/email <account entity>)
;; => <email entity>
但另一方面,如果我查看电子邮件实体的键,我不知道它已链接帐户信息。
(keys <email entity>)
;; => [:email/name :email/type]
(:account/_email <email entity>)
;; => <account entity>
如何在不反复试验的情况下发现 :account/_email
是有效 key ?
最佳答案
要检查 key 是否有效,您可以使用:
(.containsKey <email entity> :account/_email)
;; => true
为了获取所有有效的实体 key 包括反向 key :
(.touch <email entity>)
(keys (.cache <email entity>))
请注意,直接在实体上调用的(keys)
仅返回前向键。
在类似的架构上进行测试。
旁注:除了
(:account/_email <email entity>)
您还可以查询以获取已链接指定电子邮件的帐户:
(q '[:find ?a :in $ ?e :where [?a :account/email ?e] ] (db conn) (:db/id <email entity>))
关于clojure - 在datomic中,如何找出哪些键可用于反向查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629085/