Is it possible to order by a junction table? #2990
-
|
I am using the following query to get the results const { data, error } = await supabase
.from("categories")
.select(
`
category_id, category_name,
groups(
*, members(*)
)
`
)
.order("group_order", {
foreignTable: "category_groups",
ascending: true,
});However, the order method doesn't appear to be doing anything in this example. Is there a way order by the junction table? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
Edit: Final answer here: #2990 (reply in thread) Can you give an example of the expected & actual value for |
Beta Was this translation helpful? Give feedback.
-
|
Hello @richardkyk @soedirgo @steve-chavez I was facing the same issue and made a small example with the well known
I understood from the different discussions on the subject that it is (currently?) not possible to order the result on a column of a bridge table (in a m-to-n relationship). What I ended up with is a view create or replace view books_ordered as
select
b.*,
ab.order
from
books as b
inner join author_books ab on b.id = ab.book_id
order by
ab.author_id asc,
ab.order ascWhen executing: await supabase
.from("authors")
.select(`
name,
books:books_ordered!author_books (
title, order
)
`);The result is the following: [
{
"name": "Alice",
"books": [
{
"title": "Book #1",
"order": 1
},
{
"title": "Book #2",
"order": 2
}
]
},
{
"name": "Bob",
"books": [
{
"title": "Book #3",
"order": 1
}
]
}
]The books are in the right order 🥳 I would love to hear from you to
With a bit of advance, happy new year! UPDATE Although it works so I'm happy, I'm wondering how |
Beta Was this translation helpful? Give feedback.
-
|
It works well export function useIdeaWithRelations(id: string) { }); |
Beta Was this translation helpful? Give feedback.


Edit: Final answer here: #2990 (reply in thread)
Can you give an example of the expected & actual value for
data?