You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Datomic复合元组是否支持嵌套lookup refs查找引用?

结论

你不能直接在复合元组参数里嵌套使用lookup ref,Datomic不会递归解析参数数组内的lookup ref。

原因

Datomic仅会对直接用作实体标识的lookup ref做自动解析转换为对应db/id,你传入的[[:shelf/name "my-shelf"] "book-1"]会被视为普通二元数组,和数据库中存储的[<shelf实体id>, "book-id"]格式的复合元组无法匹配,因此返回空结果。

正确实现方式

如果你希望同时用到:book/shelf+book的索引加速查询,推荐在查询内先完成shelf lookup ref的解析,再构造匹配的复合元组:

(d/q '[:find ?b ?id
       :in $ ?shelf-name ?book-id
       :where
       [?s :shelf/name ?shelf-name]
       [(vector ?s ?book-id) ?shelf+book]
       [?b :book/shelf+book ?shelf+book]
       [?b :book/id ?id]]
     db "my-shelf" "book-1")

如果不需要刻意使用复合元组索引,也可以直接拆成两个属性条件查询,逻辑更直观:

(d/q '[:find ?b ?id
       :in $ ?shelf-name ?book-id
       :where
       [?s :shelf/name ?shelf-name]
       [?b :book/shelf ?s]
       [?b :book/id ?book-id]
       [?b :book/id ?id]]
     db "my-shelf" "book-1")

内容的提问来源于stack exchange,提问作者Cesar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 13:36:03