我尝试了相同代码的许多不同变体,试图让supabase客户端从route_cache表中获取数据,使用fetch_route函数而不是。
drop function public.fetch_route;
CREATE OR REPLACE FUNCTION public.fetch_route(destinationWKT text, sourceWKT text)
RETURNS SETOF route_cache AS $$
BEGIN
RETURN QUERY SELECT * FROM route_cache
WHERE ST_Equals(source::geometry, ST_GeomFromText(sourceWKT, 4326)::geometry)
AND ST_Equals(destination::geometry, ST_GeomFromText(destinationWKT, 4326)::geometry);
END;
$$ LANGUAGE plpgsql;
这就是功能。
let sourceWKT = "POINT(\(source.longitude) \(source.latitude))"
let destinationWKT = "POINT(\(destination.longitude) \(destination.latitude))"
let data = RouteCacheResponse(sourceWKT: sourceWKT, destinationWKT: destinationWKT)
do {
print(sourceWKT)
print(destinationWKT)
let response = try await supabase.database.rpc("fetch_route", params: data).execute()
这是我在 swift 中的实现。
drop table route_cache;
create table
route_cache (
id uuid primary key,
source geography (point),
destination geography (point),
route geography (LINESTRING),
travelTime float,
created_at timestamp with time zone default current_timestamp
);
这是我创建表的 SQL 查询。
当我直接通过 SQL 查询在 supabase 仪表板中运行该函数时,它工作正常。
SELECT * FROM public.fetch_route(
'POINT(-73.935242 40.730610)', -- Example WKT for New York City
'POINT(-122.431297 37.773972)' -- Example WKT for San Francisco
);
只是不在 Xcode 中,它会发出此错误。
POINT(-2.5872552394866943 51.45983123779297)
POINT(-2.590308666229248 51.45743942260742)
Error fetching route: Could not find the function public.fetch_route(destinationWKT, sourceWKT) in the schema cache
我很困惑,请帮忙
我尝试了你的代码,如果你将
RouteCacheResponse
结构定义更改为使用小写字段名称,它就可以工作。
示例:
struct RouteCacheResponse: Codable {
let sourcewkt: String
let destinationwkt: String
}
发生这种情况是因为当您创建函数时 Postgres 会将参数转换为小写。
所以你有两种方法来克服这个问题:
CREATE OR REPLACE FUNCTION public.fetch_route("destinationWKT" text, "sourceWKT" text)
希望能为您澄清,如果您仍有问题,请告诉我。