如何使用 json 插入非空列的 table?
How to insert into a table with not null columns using json?
我正在尝试插入具有多个非空默认列的 table food
,使用如下命令:
food_insertone('{"id": 1, "taste": "sweet"}'::JSON)
food_insertone('{"id": 2}'::JSON)
food_insertone('{"id": 3, "taste": null}'::JSON)
结果应该是这样的:
INSERTED 1, 'sweet'
INSERTED 2, ''
ERROR (null not allowed in taste)
tablefood
定义为:
CREATE TABLE "food" (
"id" INT,
"taste" TEXT NOT NULL DEFAULT '',
...
);
CREATE OR REPLACE FUNCTION "food_insertone" (JSON)
RETURNS VOID AS $$
INSERT INTO "food" SELECT * FROM json_populate_record(NULL::"food", );
$$ LANGUAGE SQL;
我正在尝试插入为:
SELECT food_insertone('{"id": 1}'::JSON);
但这不起作用并给我一个错误:
null value in column "taste" violates not-null constraint
我知道 json_populate_record()
为 JSON 中未提及的列创建 NULL 值,这导致插入 NULL,从而导致此错误。普通插入可以工作,但这是动态的 table.
使用默认值简单案例:
t=# create table food(id int, t text not null default 'some');
CREATE TABLE
t=# insert into food(id) SELECT id FROM json_populate_record(NULL::"food", '{"id":0}');
INSERT 0 1
t=# select * from food ;
id | t
----+------
0 | some
(1 row)
使用合并和另一个值:
t=# insert into food(id,t)
SELECT id,coalesce(t,'some simple other value')
FROM json_populate_record(NULL::"food", '{"id":0}');
当然你可以使用一些可怕的方法来获得实际的默认值:
t=# insert into food(id,t) SELECT id,coalesce(t,rtrim) FROM json_populate_record(NULL::"food", '{"id":0}') join (select rtrim(ltrim(split_part(column_default,'::',1),$$'$$),$$'$$) from information_schema.columns where table_name = 'food' and column_name = 't') dflt on true;
INSERT 0 1
t=# select * from food ;
id | t
----+-------------------------
0 | some simple other value
0 | some
(2 rows)
我正在尝试插入具有多个非空默认列的 table food
,使用如下命令:
food_insertone('{"id": 1, "taste": "sweet"}'::JSON)
food_insertone('{"id": 2}'::JSON)
food_insertone('{"id": 3, "taste": null}'::JSON)
结果应该是这样的:
INSERTED 1, 'sweet'
INSERTED 2, ''
ERROR (null not allowed in taste)
tablefood
定义为:
CREATE TABLE "food" (
"id" INT,
"taste" TEXT NOT NULL DEFAULT '',
...
);
CREATE OR REPLACE FUNCTION "food_insertone" (JSON)
RETURNS VOID AS $$
INSERT INTO "food" SELECT * FROM json_populate_record(NULL::"food", );
$$ LANGUAGE SQL;
我正在尝试插入为:
SELECT food_insertone('{"id": 1}'::JSON);
但这不起作用并给我一个错误:
null value in column "taste" violates not-null constraint
我知道 json_populate_record()
为 JSON 中未提及的列创建 NULL 值,这导致插入 NULL,从而导致此错误。普通插入可以工作,但这是动态的 table.
使用默认值简单案例:
t=# create table food(id int, t text not null default 'some');
CREATE TABLE
t=# insert into food(id) SELECT id FROM json_populate_record(NULL::"food", '{"id":0}');
INSERT 0 1
t=# select * from food ;
id | t
----+------
0 | some
(1 row)
使用合并和另一个值:
t=# insert into food(id,t)
SELECT id,coalesce(t,'some simple other value')
FROM json_populate_record(NULL::"food", '{"id":0}');
当然你可以使用一些可怕的方法来获得实际的默认值:
t=# insert into food(id,t) SELECT id,coalesce(t,rtrim) FROM json_populate_record(NULL::"food", '{"id":0}') join (select rtrim(ltrim(split_part(column_default,'::',1),$$'$$),$$'$$) from information_schema.columns where table_name = 'food' and column_name = 't') dflt on true;
INSERT 0 1
t=# select * from food ;
id | t
----+-------------------------
0 | some simple other value
0 | some
(2 rows)