Postgres array of objects error `error: could not determine polymorphic type because input has type "unknown"`
Postgres array of objects error `error: could not determine polymorphic type because input has type "unknown"`
我一直在尝试遍历 postgres 中的对象数组。但是,我反复收到错误 error: could not determine polymorphic type because input has type "unknown"
。我认为这与我传递给函数的值有关,看起来像这样。
[
{
"number": 1,
"letter": "a"
},
{
"number": 2,
"letter": "b"
}
]
函数如下。我已经制作了 ANYARRAY 类型,我认为它应该完全按照名称所暗示的那样进行。我不介意有更具体的选项可用。
CREATE OR REPLACE FUNCTION "CheckArrayOfObjects" (
"@arrayOfObjects" ANYARRAY
)
RETURNS void AS
$func$
DECLARE "e" JSONB;
BEGIN
FOR "e" IN json_array_elements("@arrayOfObjects")
LOOP
RAISE NOTICE 'Checking Item %', "e";
END LOOP;
END;
$func$ LANGUAGE PLPGSQL;
我也尝试过 FOREACH,但效果并不好。我认为问题出在传递的值上,所以现在还不确定修改循环是否会做任何事情。
参数的类型应该是JSONB
(或JSON
):
CREATE OR REPLACE FUNCTION "CheckArrayOfObjects" ("@arrayOfObjects" JSONB)
RETURNS void AS
$func$
DECLARE "e" JSONB;
BEGIN
FOR "e" IN SELECT jsonb_array_elements("@arrayOfObjects")
LOOP
RAISE NOTICE 'Checking Item %', "e";
END LOOP;
END;
$func$ LANGUAGE PLPGSQL;
使用示例:
SELECT "CheckArrayOfObjects"(
'[
{
"number": 1,
"letter": "a"
},
{
"number": 2,
"letter": "b"
}
]')
NOTICE: Checking Item {"letter": "a", "number": 1}
NOTICE: Checking Item {"letter": "b", "number": 2}
更新
Where do notices get logged with SQL?
取决于服务器配置参数log_min_messages (enum)
What is the type ANYARRAY for if not for arrays?
它适用于 Postgres 数组类型,例如text[]、int[] 等 Json 数组不是 Postgres 数组,参见
我一直在尝试遍历 postgres 中的对象数组。但是,我反复收到错误 error: could not determine polymorphic type because input has type "unknown"
。我认为这与我传递给函数的值有关,看起来像这样。
[
{
"number": 1,
"letter": "a"
},
{
"number": 2,
"letter": "b"
}
]
函数如下。我已经制作了 ANYARRAY 类型,我认为它应该完全按照名称所暗示的那样进行。我不介意有更具体的选项可用。
CREATE OR REPLACE FUNCTION "CheckArrayOfObjects" (
"@arrayOfObjects" ANYARRAY
)
RETURNS void AS
$func$
DECLARE "e" JSONB;
BEGIN
FOR "e" IN json_array_elements("@arrayOfObjects")
LOOP
RAISE NOTICE 'Checking Item %', "e";
END LOOP;
END;
$func$ LANGUAGE PLPGSQL;
我也尝试过 FOREACH,但效果并不好。我认为问题出在传递的值上,所以现在还不确定修改循环是否会做任何事情。
参数的类型应该是JSONB
(或JSON
):
CREATE OR REPLACE FUNCTION "CheckArrayOfObjects" ("@arrayOfObjects" JSONB)
RETURNS void AS
$func$
DECLARE "e" JSONB;
BEGIN
FOR "e" IN SELECT jsonb_array_elements("@arrayOfObjects")
LOOP
RAISE NOTICE 'Checking Item %', "e";
END LOOP;
END;
$func$ LANGUAGE PLPGSQL;
使用示例:
SELECT "CheckArrayOfObjects"(
'[
{
"number": 1,
"letter": "a"
},
{
"number": 2,
"letter": "b"
}
]')
NOTICE: Checking Item {"letter": "a", "number": 1}
NOTICE: Checking Item {"letter": "b", "number": 2}
更新
Where do notices get logged with SQL?
取决于服务器配置参数log_min_messages (enum)
What is the type ANYARRAY for if not for arrays?
它适用于 Postgres 数组类型,例如text[]、int[] 等 Json 数组不是 Postgres 数组,参见