This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-27
Channels
- # aleph (1)
- # announcements (5)
- # babashka (13)
- # beginners (68)
- # brompton (7)
- # calva (10)
- # cider (1)
- # clara (15)
- # cljsrn (2)
- # clojure (63)
- # clojure-austin (1)
- # clojure-europe (44)
- # clojure-france (2)
- # clojure-nl (5)
- # clojure-norway (1)
- # clojure-poland (1)
- # clojure-uk (8)
- # clojurescript (8)
- # clojureverse-ops (7)
- # conjure (13)
- # core-async (27)
- # cryogen (10)
- # cursive (17)
- # datomic (13)
- # deps-new (1)
- # events (1)
- # fulcro (3)
- # gratitude (4)
- # helix (6)
- # honeysql (6)
- # introduce-yourself (1)
- # jobs (2)
- # malli (13)
- # meander (9)
- # music (1)
- # news-and-articles (2)
- # off-topic (8)
- # pedestal (1)
- # reitit (4)
- # sci (25)
- # shadow-cljs (13)
- # spacemacs (2)
- # tools-build (5)
- # tools-deps (20)
- # vscode (50)
- # xtdb (2)
Hi! If I remember correctly, datalog has some way to traverse references in reverse, using _
in the attribute name, is that correct? I could not find where in the docs it is explained.
I see it e.g. in the https://github.com/cognitect-labs/day-of-datomic-cloud/blob/385438c4d983d9855bf40d83eaabb618048a7cfc/tutorial/query_tour.clj#L75. And my experiment seems to prove my suspicion that using attribute names (even non-ref) starting with _
will lead to weird errors. Yet I do not see that mentioned https://docs.datomic.com/on-prem/schema/schema.html#required-schema-attributes?!
it’s (briefly) mentioned here: https://docs.datomic.com/on-prem/query/pull.html#reverse-lookup
Thank you! It would be nice if the docs mentioned that attribute local names must not start with _ ...
Q2: I need to write a function that returns a query for a later execution. The challenge is that I need to hardcode the argument it gets - a set of IDs - into the query itself. In SQL I would do (fn [ids] (str "SELECT * FROM TableX WHERE id IN (" (s/join "," ids) ")"))
So far I have
(defn by-ids [ids]
'[:find (pull ?e [*])
:where [?e :reference/$id ?id]
:in $ [?id ...]]) ; FIXME how to include the provided ids in the query?
I am sure it is trivial but I just know too little... Any help is much appreciated!
Perhaps do [(#{ids} ?id)]
and drop the input? <-- worked
Update: As per @schmee suggestion, I ended up with [... :where '[?e :reference/$id ?id] [(ground ids) '[?id ...]]]
I believe that the right way to do this is to use multiple inputs: https://docs.datomic.com/on-prem/query/query.html#multiple-inputs
Multiple inputs would work if I had the input available when I am executing the query (since it is d/q
that takes the arguments) but that is not the case, I need to bind this input when I create the query (as data). I.e. my function takes the ids as an argument and must return a query.
hmm… maybe https://docs.datomic.com/on-prem/query/query.html#ground can be helpful here?
Thank you! That indeed works, and I expect it is more performant than using a fn
I would not discover this without you!
What is the right way to write the query "Give me the references that have the given component-id as either their source or target"? One way I guess is
[:find '?e :where ['?t :component/id component-id], '[?e ?attr ?t] '[(ground #{:reference/source :reference/target}) [?attr ...])]
another would be
[:find '?e :where '(or (and [?e :reference/source ?t] ['?t :component/$id component-id])
(and [?e :reference/target ?t] ['?t :component/$id component-id]))]
though I do not like the duplication in the latter 🙏[:find ?e
:in $ ?id
:where
[?c :component/id ?id]
(or [?e :reference/attr ?c]
[?c :reference/attr ?e])]
If you are looking for graph traversal, see:
https://forum.datomic.com/t/how-to-do-graph-traversal-with-rules/132