This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
Hello, Folks. I hope everyone's doing good =]
Is there any way to do a get-else
for a cardinality many relations? Here's a silly query just to exemplify what I want. Basically I want all the persons from a list of ids and I want their houses, and return an empty map/EntityMap if the person doesn't have any house.
(d/q '[:find ?person ?house
:in $ [?person ...]
:where
[?person :person/has-doc? true]
[(get-else $ ?house :house/persons {}) ?person] (d/db conn) ids)
I’ve found the previous conversations on this topic and yeah, I don’t think there’s a one fits all workaround for this. Gladly I found another solution to my problem that doesn’t involve the field I was trying to retrieve, so at least I got that haha
[:find ?person ?house
:in $ [?person ...]
:where
[?person :person/has-doc? true]
(or-join [[?person] ?house]
(and
(not [_ :house/persons ?person])
[(ground -1) ?house])
[?house :house/persons ?person])]
The reason get-else
doesn’t work on cardinality-many or reverse-refs (amounts to the same thing--their cardinality is not constrained) is that you need some sentinel to represent “no items” which changes the number of relations.
Result set would be #{[person-id -1]}
if no house, but #{[person-id house1-id] [person-id house2-id] ...}
if houses.
That's really nice, while searching I did found some people using a combination of or-join and ground to achieve this. But I couldn't make it work for me. Your solution does exactly what I was trying to do. Even tho I've found another solution that doesn't involve running this query, is nice to know a little more, thanks!