xephyr
05/08/2022, 10:53 AMmaybeSingle()
errors when 0 rows are returned? I thought that was the whole point of it over .single()
I've got a many:many join table called `teams_users`:
ts
id // auto-increment
created_at // NOW()
team_id // foreign key teams.id
user_id // foreign key users.id
When I do the following:
ts
supabase
.from("teams_users")
.select("id")
.match({ team_id, user_id })
.maybeSingle();
I get an 406 Not Acceptable
error:
json
{
"message":"JSON object requested, multiple (or no) rows returned",
"details":"Results contain 0 rows, application/vnd.pgrst.object+json requires 1 row"
}
The docs are pretty barebones atm so difficult to learn from but includes this quote:
> Result must be at most one row (e.g. using eq on a UNIQUE column), otherwise this will result in an error.
The e.g. using eq on a UNIQUE column
strikes me as just an example, can maybeSingle()
ONLY be used after passing a primary key in to .match/.eq
?Needle
05/08/2022, 10:53 AMxephyr
05/08/2022, 10:56 AMts
const { data, error } = await supabase
.from("teams_users")
.select("id")
.match({ team_id, user_id });
if ((data || []).length > 0) doTheThings();
as opposed to the neater and preferred:
ts
const { data, error } = await supabase
.from("teams_users")
.select("id")
.match({ team_id, user_id })
.maybeSingle();
if (data) doTheThings();
xephyr
05/08/2022, 10:56 AMgaryaustin
05/08/2022, 3:33 PMmaybeSingle(): PromiseLike<PostgrestMaybeSingleResponse<T>> {
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
const _this = new PostgrestTransformBuilder(this)
_this.then = ((onfulfilled: any, onrejected: any) =>
this.then((res: any): any => {
if (res.error?.details?.includes('Results contain 0 rows')) {
return onfulfilled({
error: null,
data: null,
count: res.count,
status: 200,
statusText: 'OK',
body: null,
})
}
return onfulfilled(res)
}, onrejected)) as any
return _this as PromiseLike<PostgrestMaybeSingleResponse<T>>
}
Needle
05/08/2022, 3:33 PMxephyr
05/09/2022, 9:12 AMgaryaustin
05/09/2022, 12:35 PMxephyr
05/09/2022, 1:00 PM.limit(1)
or omit the modifier entirely and just handle it as I did above? (.length > 0
etc)garyaustin
05/09/2022, 1:07 PMgaryaustin
05/09/2022, 1:08 PM