为什么json_populate_record()不能识别行types?
我有2个表type
和name
。 我试图通过一个函数与json_populate_record()插入到这些。 收到以下错误:
first argument of json_populate_record must be a row type
表type
创build使用:
CREATE TABLE IF NOT EXISTS "type" ( "id" TEXT NOT NULL, "value" TEXT NOT NULL, PRIMARY KEY ("id"), CHECK ("id"<>'' AND "value"<>'') ); CREATE INDEX IF NOT EXISTS "idx_type_value" ON "type" ("value"); CREATE OR REPLACE FUNCTION "type_insertone" ( IN _a JSON ) RETURNS VOID AS $$ BEGIN INSERT INTO "type" SELECT * FROM json_populate_record(NULL::"type", _a); END; $$ LANGUAGE plpgsql;
表格name
是使用
CREATE TABLE IF NOT EXISTS "name" ( "id" TEXT NOT NULL, "value" TEXT NOT NULL, PRIMARY KEY ("id"), CHECK ("id"<>'' AND "value"<>'') ); CREATE INDEX IF NOT EXISTS "idx_name_value" ON "name" ("value"); CREATE OR REPLACE FUNCTION "name_insertone" ( IN _a JSON ) RETURNS VOID AS $$ BEGIN INSERT INTO "name" SELECT * FROM json_populate_record(NULL::"name", _a); END; $$ LANGUAGE plpgsql;
何时调用type_insertone()
{id: 'x', 'value': 'x'}
,它的工作原理。 但是,当我做同样的name_insertone()
它不工作!
我在想,如果这个函数是在表创build之前创build的,这可能导致它不知道表name
,但是我认为不是这种情况。
我不知道我应该在这里尝试。 我也检查过,这个name
不是保留的关键字,所以是type
。
“名称”是文本types和关键词 :
例如:
t=# alter table name rename to name1; ALTER TABLE t=# CREATE OR REPLACE FUNCTION "name_insertone" ( IN _a JSON ) RETURNS VOID AS $$ BEGIN INSERT INTO "name1" SELECT * FROM json_populate_record(NULL::"name1", _a); END; $$ LANGUAGE plpgsql; CREATE FUNCTION t=# select name_insertone('{"id": "x", "value": "x"}'); name_insertone ---------------- (1 row)
但是:
t=# create table text(i int); CREATE TABLE t=# SELECT * FROM json_populate_record(NULL::"text", '{}') t-# ; ERROR: first argument of json_populate_record must be a row type t=# create table char(i int); CREATE TABLE t=# SELECT * FROM json_populate_record(NULL::"char", '{}'); ERROR: first argument of json_populate_record must be a row type