Postgres 存储过程在解析 JSONB 对象时出错
Postgres stored procedure errs while parsing JSONB object
我正在尝试 运行 Postgres 中的以下存储过程:
...
FOR showing IN SELECT * FROM json_to_recordset(to_json(event_times)) AS show(id INTEGER,
times JSONB, startDate DATE, endDate DATE) LOOP
IF showing.id > 0 THEN
UPDATE
event_shows
SET
start_date = showing.startDate, end_date = showing.endDate, times = showing.times
WHERE
event_id = eid AND
id = showing.id;
ELSE
INSERT INTO
event_shows (event_id, start_date, end_date, times)
VALUES
(eid, showing.startDate, showing.endDate, showing.times);
END IF;
END LOOP;
event_times
的值为 [{"times":[{"end":"13:00","start":"12:00"}],"endDate":"2020-05-19T19:45:47.121Z","startDate":"2020-05-19T19:45:47.121Z"},{"startDate":"2020-05-20T19:55:15.000Z","endDate":"2020-05-20T19:55:15.000Z","times":[{"start":"12:00","end":"13:00"}]}]
。
当我 运行 代码时,它出错了:
"SQL statement \"INSERT INTO\n event_shows (event_id, start_date, end_date, times)\n VALUES\n (eid, showing.startDate, showing.endDate, showing.times)\"
消息是null value in column \"start_date\" violates not-null constraint
。似乎我的 JSONB 对象没有被正确解析。
我做错了什么?
tableevent_shows
定义为:
CREATE TABLE event_shows (
id SERIAL,
event_id SERIAL NOT NULL REFERENCES event(id),
start_date DATE NOT NULL,
end_date DATE NOT NULL,
times JSONB,
PRIMARY KEY (id)
);
运行: PostgreSQL 10.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 6.4.0) 6.4.0, 64-bit
基本上,Postgres 中的列名是敏感的,因此无论何时使用驼峰命名,如 startDate
,都应该用引号引起来。 Postgres版本无所谓。
我正在尝试 运行 Postgres 中的以下存储过程:
...
FOR showing IN SELECT * FROM json_to_recordset(to_json(event_times)) AS show(id INTEGER,
times JSONB, startDate DATE, endDate DATE) LOOP
IF showing.id > 0 THEN
UPDATE
event_shows
SET
start_date = showing.startDate, end_date = showing.endDate, times = showing.times
WHERE
event_id = eid AND
id = showing.id;
ELSE
INSERT INTO
event_shows (event_id, start_date, end_date, times)
VALUES
(eid, showing.startDate, showing.endDate, showing.times);
END IF;
END LOOP;
event_times
的值为 [{"times":[{"end":"13:00","start":"12:00"}],"endDate":"2020-05-19T19:45:47.121Z","startDate":"2020-05-19T19:45:47.121Z"},{"startDate":"2020-05-20T19:55:15.000Z","endDate":"2020-05-20T19:55:15.000Z","times":[{"start":"12:00","end":"13:00"}]}]
。
当我 运行 代码时,它出错了:
"SQL statement \"INSERT INTO\n event_shows (event_id, start_date, end_date, times)\n VALUES\n (eid, showing.startDate, showing.endDate, showing.times)\"
消息是null value in column \"start_date\" violates not-null constraint
。似乎我的 JSONB 对象没有被正确解析。
我做错了什么?
tableevent_shows
定义为:
CREATE TABLE event_shows (
id SERIAL,
event_id SERIAL NOT NULL REFERENCES event(id),
start_date DATE NOT NULL,
end_date DATE NOT NULL,
times JSONB,
PRIMARY KEY (id)
);
运行: PostgreSQL 10.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 6.4.0) 6.4.0, 64-bit
基本上,Postgres 中的列名是敏感的,因此无论何时使用驼峰命名,如 startDate
,都应该用引号引起来。 Postgres版本无所谓。