https://supabase.com/ logo
#help
Title
# help
f

frubalu

09/16/2021, 5:39 PM
Does anyone know how to order nested data from a supabase select?
Copy code
const { data, error } = await supabase
      .from('packs')
      .select(
        `
          *,
          categories(*, category_items(*, item:item_id(*)))
        `
      )
      .order('created_at', { ascending: true })
      .order('created_at', { foreignTable: 'categories' })
      .order('position', { foreignTable: 'category_items', ascending: false })
      .eq('user_id', token.sub);
Essentially I am selecting from the packs table, all of the related categories, category_items and items. I'd like for the nested category_items to be ordered by position, however they're all coming in totally out of order for some reason
m

Mattias

09/17/2021, 8:46 AM
I have the same problem. Can anyone post a simple and working example including tables? https://supabase.io/docs/reference/javascript/order does not help me.
f

frubalu

09/17/2021, 3:16 PM
Hey @User I think I figured it out. Here's how I modified my original call:
Copy code
const { data: packsData, error: packsError } = await supabase
      .from('packs')
      .select(
        `
          *,
          categories(*, category_items(*, item:item_id(*)))
        `
      )
      .order('created_at', { ascending: true })
      .order('created_at', { foreignTable: 'categories' })
      .order('position', {
        foreignTable: 'categories.category_items',
        ascending: true,
      })
      .eq('user_id', token.sub);
notice the last
.order
, I'm specifying the foreignTable as
categories.category_items
m

Mattias

09/20/2021, 9:43 AM
Hi @User! Here is my problem:
Copy code
const { data } = await supabase
        .from('customers_subscriptions')
        .select('
            id,
            customer (first_name, last_name),
            subscription (name)')
        .order('customer', { foreignTable: 'customers.last_name' });
Table called 'customers'
Table called 'customers_subscriptions'
I would like data to be ordered by the customers last name. Please help 😫
f

frubalu

09/20/2021, 3:10 PM
Hey @User , I'm not 100% sure (it's been more trial-and-error for me), but have you tried
.order('last_name', { foreignTable: 'customers' })
?
Feel free to direct message me if that still doesn't work
m

Mattias

09/21/2021, 9:49 AM
Thanks @User but it didn't work.
I guess I need to order it on the client-side...
...
Copy code
const inDeliveryOrder = data.sort(function (a, b) {
        return a.customer.delivery_order - b.customer.delivery_order;
    });
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort