Unable to Filter on Multiple Conditions Across Related Tables in Supabase Query #3663
-
Issue DescriptionI am experiencing an issue when trying to filter results from the Related DiscussionsI have tried various solutions, including discussions and issues from the following links: #787, #6778, #18997, PostgREST Issue #1075 const baseQuery = supabase
.from('bookings')
.select(
`id, inserted_at,
dispatch_client_org_name:dispatch_client_orgs!inner(id, name, avatar),
dispatcher_name:user_profiles!dispatcher(user_id, name), dropoff_date`,
{ count: 'exact' }
)
.eq('dispatch_org_id', dispatch_org_id)
.order('inserted_at', { ascending: false });
if (searchTerm) {
baseQuery.or(
`load_number.ilike.%${searchTerm}%,pickup_city.ilike.%${searchTerm}%,dispatch_client_orgs.name.ilike.%${searchTerm}%`
);
} Expected BehaviorI expect to retrieve records from the
Actual BehaviorWhen attempting to combine these filters using the if (searchTerm) {
baseQuery.or(
`load_number.ilike.%${searchTerm}%,pickup_city.ilike.%${searchTerm}%,dispatch_client_orgs.name.ilike.%${searchTerm}%`
);
} This results in the following error:
Working Conditions
Steps to Reproduce
if (searchTerm) {
Additional InformationI have attempted various solutions and consulted multiple resources, including Stack Overflow and the Supabase documentation, but none have resolved the issue. Any guidance or suggestions for a fix would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I think this is pretty similar to this Discussion: #3333. The baseQuery
.ilike('dispatch_client_orgs.name', `%${searchTerm}%`)
.or(
`load_number.ilike.%${searchTerm}%,pickup_city.ilike.%${searchTerm}%,dispatch_client_org_name.not.is.null`
); You may also need to add another empty embedding and use it for the filter instead (there's an example in the discussion too). The PostgREST documentation has examples for these cases, although for two embedded resources. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your feedback! Here’s the updated query based on your suggestions:
const baseQuery = supabase
.from('bookings')
.select(
'id, dco_filter:dispatch_client_orgs!inner(), dispatch_client_org_name:dispatch_client_orgs!inner(id, name, avatar), dispatcher_name:user_profiles!dispatcher(user_id, name), user_profiles!driver(user_id, phone, name)), invoice:invoices(id)',
{ count: 'exact' }
)
.eq('dispatch_org_id', dispatch_org_id)
.order('inserted_at', { ascending: false });
if (searchTerm) {
baseQuery
.ilike('dco_filter.name', `%${searchTerm}%`)
.or(
`load_number.ilike.%${searchTerm}%, pickup_city.ilike.%${searchTerm}%, dco_filter.not.is.null`
);
}
const { data, error } = await baseQuery;
if (error) {
console.error("Error fetching data:", error);
} else {
console.log("Fetched data:", data);
} Issue EncounteredWhen using //Response
{
"data": [],
"count": 0
} |
Beta Was this translation helpful? Give feedback.
Ah, I made a mistake here. It must be due to the
!inner
in the filter, it should be removed:Check if that works.