Closed bbagherian closed 1 year ago
Running as query is ok by the way, the api call is broken I believe:
Running as query is ok by the way
Try it in SQL as:
select * from nearby_restaurants(40, -73);
And you'll see it fail in the same way as the RPC.
ERROR: a column definition list is required for functions returning "record"
When you declare a SETOF
function, it is a table-valued function and thus a SELECT * FROM func()
is the most suitable query for it.
Additionally:
select nearby_restaurants()
result. It has the tuple format (3, \"..)
which is not suitable for a web application.Although a better error message could be generated.
The root cause of this issue seems to be this guide https://supabase.com/docs/guides/database/extensions/postgis#order-by-distance
This discussion reports it used to work before? Will check that out.
Tracking this on https://github.com/PostgREST/postgrest/issues/2881
I'll fix this on https://github.com/PostgREST/postgrest/issues/2881 and go back to the previous behavior. So this worked before because PostgREST considered a setof record
as scalar to avoid OP's error.
However this method has the disadvantage of not being able to select
particular columns from it:
create or replace function setof_record() returns setof record as $$
select * from projects;
$$ language sql;
curl 'localhost:3000/rpc/setof_record?select=id'
{"code":"42703","details":null,"hint":null,"message":"column setof_record.id does not exist"}
Considering that, it would be better to fix the PostGIS guide to use this function signature:
create or replace function nearby_restaurants(lat float, long float)
returns TABLE (id public.restaurants.id%TYPE, name public.restaurants.name%TYPE, location text, dist_meters float)
language sql
as $$
select id, name, st_astext(location) as location, st_distance(location, st_point(long, lat)::geography) as dist_meters
from public.restaurants
order by location <-> st_point(long, lat)::geography;
$$;
The above should work on the current version.
I'll fix this on PostgREST/postgrest#2881 and go back to the previous behavior. So this worked before because PostgREST considered a
setof record
as scalar to avoid OP's error.However this method has the disadvantage of not being able to
select
particular columns from it:create or replace function setof_record() returns setof record as $$ select * from projects; $$ language sql;
curl 'localhost:3000/rpc/setof_record?select=id' {"code":"42703","details":null,"hint":null,"message":"column setof_record.id does not exist"}
Considering that, it would be better to fix the PostGIS guide to use this function signature:
create or replace function nearby_restaurants(lat float, long float) returns TABLE (id public.restaurants%TYPE, name public.restaurants%TYPE, location text, dist_meters float) language sql as $$ select id, name, st_astext(location) as location, st_distance(location, st_point(long, lat)::geography) as dist_meters from public.restaurants order by location <-> st_point(long, lat)::geography; $$;
The above should work on the current version.
Thanks for the solution. Btw I changed a little this because it was returning invalid sql error then I removed public schema name and wrote types hardcoded instead of variables (%TYPE) and here it's:
create or replace function nearby_restaurants(lat float, long float)
returns TABLE (id int, name varchar, location text, dist_meters float)
language sql
as $$
select id, name, st_astext(location) as location, st_distance(location, st_point(long, lat)::geography) as dist_meters
from restaurants
order by location <-> st_point(long, lat)::geography;
$$;
FYI, this is already fixed on PostgREST v11.2.0 (not deployed to Supabase yet).
Deploy with the fix is already approved https://github.com/supabase/postgres/pull/720.
Additionally the docs have been updated https://github.com/supabase/supabase/pull/16177
Hello, i have this problem that suddenly appear on my app. When will the fix be deployed in supabase?
Bug report
Describe the bug
Following this documentation leads to an error on
rpc
call: https://supabase.com/docs/guides/database/extensions/postgisTo Reproduce
SQL on the website dashboard:
and now in the app:
Expected behavior
List of results
Screenshots
System information