【问题标题】:Postgresql v12 Function returns Function Name in JsonPostgresql v12 函数在 Json 中返回函数名称
【发布时间】:2019-12-17 19:40:26
【问题描述】:

这是函数所需的查询结果:

[{"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】:

    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

    【讨论】:

    • 我已经尝试过您建议的解决方案。很抱歉没有说明我正在使用 Postgrest。谢谢。
    • 我相信他调用函数 OK,但 PostgREST 似乎将函数结果包装为一个数组属性(可能是假设行),其名称是函数名称。
    猜你喜欢
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2016-09-02
    • 2013-04-11
    • 2012-01-09
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    相关资源
    最近更新 更多