I'm using Supabase's query builder to fetch data with a foreign key relationship, but the query is only returning one row, even though I expect it to return two.
Here's the code I'm using:
const { data, error } = await supabase
.from('m_service_upsells')
.select(`*, upsell_service:m_services!m_service_upsells_upsell_service_id_fkey(*)`)
.eq('main_service_id', main_service_id);
Question: Why is the Supabase query builder returning only one row when two rows exist in the database? Is there a limitation or something I’m missing in the query builder's syntax for handling relationships?
Table Schema (simplified):
m_service_upsells: { id, main_service_id, upsell_service_id }
m_services: { id, name, description }
Relationship: m_service_upsells.upsell_service_id -> m_services.id
Any insights or suggestions would be greatly appreciated!
What I’ve Checked:
- Database Data: I’ve confirmed that the database contains two rows with the same main_service_id.
- Foreign Key: The foreign key relationship is correctly defined and works fine in raw SQL.
- SQL Query: I ran the equivalent raw SQL query, and it works perfectly:
SELECT
m_service_upsells.*,
json_agg(m_services.*) AS upsell_service
FROM
m_service_upsells
LEFT JOIN
m_services
ON
m_service_upsells.upsell_service_id = m_services.id
WHERE
m_service_upsells.main_service_id = 'your_main_service_id'
GROUP BY
m_service_upsells.id;
Observations:
- No errors are returned from the query builder.
- The
select()query seems to fetch only the first matching row