Is it possible to join a table with itself with su...
# sql
j
Is it possible to join a table with itself with supabase-js? I have this right now, but I'm unsure what I need to do to make it work:
Copy code
javascript
  const { data, error } = await db
    .from('source_products')
    .select('*, source_products!inner(*)')
    .eq('id', id)
    .eq('source_products.main_sku', 'how do I reference this')
    .limit(1)
    .maybeSingle()
Basically I want to join in other products that have the same value for
main_sku
. Do I need to alias both 'tables' (it's the same table of course) This works:
Copy code
sql
select t1.*, t2.* from source_products t1 left join source_products t2 on t1.main_sku = t2.main_sku where t1.id = '4bfbf03d-9707-4b03-ac44-04fb7296a91a'
How do I do the same with supabase-js?
s
Is
main_sku
a foreign key to
source_products
?
self joins do work but they have some gotchas, see https://github.com/supabase/supabase/discussions/4581
j
Thanks a lot for the hint. I decided to do it with a postgres function since the complete query is a bit more complex than what I shared above