Fork me on GitHub
#xtdb
<
2023-09-22
>
Chris Lester06:09:59

I am attempting to create a query that returns a list of all children grouped by their parent. The query below isn't returning results and I'm unsure what I don't understand. Is this incorrect, assuming an :entry/parent attribute exists and has the id of some parent?

query '{:find [e child-id]
                :where [[e :xt/id]
                        (child-of e ?child)
                        [child? :xt/id child-id]]
                :rules [[(child-of [?p] ?c)
                         [?c :entry/parent ?p]]
                        [(child-of [?p] ?c)
                         [?c :entry/parent ?t]
                         (child-of ?p ?t)]]}

tatut07:09:26

you shouldn’t need [e :xt/id] (every doc has id)

tatut07:09:52

but you have [child? :xt/id child-id] but ?child in the previous clause

tatut07:09:08

that clause should also be unnecessary, just {:find [e ?child] :where [(child-of e ?child)] …} should be enought I think

Chris Lester19:09:54

Thx @U11SJ6Q0K .. making those changes gives Index 0 out of bounds for length 0 .. which is different.

Chris Lester19:09:37

Removing the second rule returns results. I'm unsure why it would give an error so would like to understand that, but I don't need the children of children yet (which is what I think that rule is for). Eliminate that and it returns results look correct ... need to verify ofc. ... verified. Works.

Christian Pekeler07:09:51

@US893PCLF could you post the working query, please?

Chris Lester18:09:05

There is likely a better way to do this (and pull those properties in one go and project them into :entry/children) but haven't discovered that yet.