我在 RPC(遠程程序呼叫)中使用了以下函式,在該函式中我嘗試訪問 JSON 物件的可選文本欄位并將其轉換為 UUID:
CREATE OR REPLACE FUNCTION update_wcomponent (item wcomponents)
returns wcomponents
language plpgsql
security definer
SET search_path = public
as $$
DECLARE
existing wcomponents%ROWTYPE;
BEGIN
UPDATE wcomponents
SET
modified_at = now(),
title = item.json::json->'title',
type = item.json::json->'type',
-- Following line errors
attribute_id = (item.json::json->'attribute_wcomponent_id')::text::uuid,
json = item.json
WHERE id = item.id AND modified_at = item.modified_at;
-- Get the latest value
SELECT * INTO existing FROM wcomponents WHERE id = item.id;
return existing;
END;
$$;
但是,將文本轉換為 uuid 時會出錯,并出現以下錯誤:
invalid input syntax for type uuid: ""310edbfb-0af1-411b-9275-3fc87948c3a5""
我在其中發布以下結構的 JSON 物件:
{
"item": {
"base_id": 18,
"id": "27e46ec1-3d9a-412d-9807-8e08e515988d",
"json": {
"id": "27e46ec1-3d9a-412d-9807-8e08e515988d",
"title": "",
"type": "state_value",
"attribute_wcomponent_id": "310edbfb-0af1-411b-9275-3fc87948c3a5"
},
"modified_at": "2022-04-23T22:11:13.533Z"
}
}
attribute_wcomponent_id是可選的,所以當它undefined不存在時它可以正常作業。
如何成功地將它從 json 文本屬性轉換為 uuid?似乎文本字串被參考為"310edbfb-0af1-411b-9275-3fc87948c3a5"而不僅僅是310edbfb-0af1-411b-9275-3fc87948c3a5.
uj5u.com熱心網友回復:
您需要使用->>運算子將??您的attribute_wcomponent_id值回傳為text,而不是json。然后不需要轉換為text之前的型別uuid,并且您不再遇到您所看到的雙引號問題。
attribute_id = (item.json::json->>'attribute_wcomponent_id')::uuid
dbfiddle 上的演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463012.html
下一篇:顫振的wpapi
