我试图在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对象没有被正确解析。
我到底做错了什么?
表 event_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中的列名是敏感的,所以每当我们使用一个camelcase的名字时,就像 startDate
,应该用引号。Postgres版本并不太重要。