Fork me on GitHub
#datahike
<
2022-02-08
>
awb9902:02:18

I am trying to do a outer join and I cannot find a way to do it. I have :tracking/invoice which is a ref to an invoice. I would like to pull all fields from Invoice and tracking. But it might be that an invoice does not have tracking. I could do it with two separate queries. First get rhe invoice and pull all its fields. Then pull the tracking by searching via tracking/invoice and pull there. But I want to operate in thousands of invoices, so I think two queries is a bad idea. I found: lookup-ref? Perhaps this does what I need.. But I cannot find docs for it. Any ideas?

awb9902:02:12

(def xero-orders '[:find [?id ?t (pull ?t [*]) (pull ?id [*]) ] :in $ ?inv-no ; ?xxx :where [?id :xero/invoice-number ?inv-no] [?t :tracking/invoice ?id] ;[(get-else $ ?id :tracking/invoice ::no-val) ?t] ;[(?xxx $ ?e :tracking/invoice) ?t] ;[?t :tracking/tag ?tag] ])

awb9902:02:59

so this query works, but it only gives me data, when :tracking/invoice refers to an invoice. In case there is no tracking yet, then I get nil. In this case I still need the invoice data.

Björn Ebbinghaus14:02:34

Maybe I don't quite understand your case... But this looks like a case for a https://docs.datomic.com/on-prem/query/pull.html#reverse-lookup

(d/pull db ['* {:tracking/_invoice ['*]}] [:xero/invoice-number 42])
If invoice 42 has no tracking. :tracking/_invoice is nil.

awb9909:02:17

This works perfect! Thanks