Postgresql v12函数在Json中返回函数名称

问题描述 投票:0回答:1

这是来自函数的必需查询结果:

[{"id":1,"task":"Finish Task ONE"},{"id":2,"task":"Finish Task TWO"}]

并且它在pgAdmin4中通过以下方式正确生成:SELECT api.add_them(1,2)in 4。

但是,当使用Windows Curl或Android Studio应用程序调用该函数时,它会产生:

[{"add_them":[{"id":1,"task":"Finish Task ONE"},{"id":2,"task":"Finish Task TWO"}]}]

因此,函数名称被包含为主体的包装。

Windows curl脚本是:

curl http://localhost:3000/rpc/add_them -X POST ^
     -H "Authorization: Bearer <My JWT Token>"   ^
     -H "Content-Type: application/json" ^
     -H "Prefer: return=representation" ^
     -d "{\"a\": 1, \"b\": 2}"

功能是:

CREATE OR REPLACE FUNCTION api.add_them(
a integer,
b integer)
RETURNS setof json
LANGUAGE 'plpgsql'

COST 100
VOLATILE 
ROWS 1000

AS $$

BEGIN
RETURN QUERY 
SELECT array_to_json(array_agg(row_to_json(t)))
FROM ( select id, task from api.todos
       where id >= a and id <= b) t;
END;  $$;

ALTER FUNCTION api.add_them(a integer, b integer)
OWNER TO postgres;
curl rpc postgresql-json postgrest
1个回答
0
投票

PostgREST使用架构缓存,如果您添加新功能并且不刷新它,它可能会过时。

要刷新架构缓存,请重新启动PostgREST或使用SIGUSR1(killall -SIGUSR1 postgrest)重新加载它。

您可以在https://postgrest.org/en/v6.0/admin.html#schema-reloading中查看有关此内容的更多详细信息。

也请检查本节末尾的重要说明(绿色):https://postgrest.org/en/v6.0/api.html#stored-procedures

© www.soinside.com 2019 - 2024. All rights reserved.