如何将 jsonb 中的时间戳更新为函数 now() 的值?
我尝试像这样做smt
UPDATE test
SET column = jsonb_set(column,'{time}',to_char(now(),'some date format'),true)
我遇到了类似
的错误 'jsonb_set(json,unknown,text,bool) is not available'
我认为错误的原因是 now() 的值不是单引号,因为这个查询正在工作
UPDATE test
SET column = jsonb_set(column,'{time}','date with the same date format ',true)
有什么解决办法吗?
jsonb_set(json,unknown,text,bool) is not available
PostgreSQL 找不到具有此签名的函数。根据 docs,签名是
jsonb_set(jsonb, text[], jsonb, boolean)
。
to_char()
正在返回 text
,您需要 jsonb
,并且演员表应该可以解决问题:
jsonb_set(column, '{time}', to_char(now(),'\"some date format\"')::jsonb, true)
(请记住,
::jsonb
需要有效的 JSON 作为输入,例如字符串周围的 "
。)
用
to_char(...)
结束 to_jsonb()
通话:
test# DROP TABLE IF EXISTS t;
DROP TABLE
test#CREATE TABLE t (col JSONB);
CREATE TABLE
test# INSERT INTO t (col) VALUES ('{"time": null}');
INSERT 0 1
test# TABLE t;
col
════════════════
{"time": null}
(1 row)
test# UPDATE t SET col = jsonb_set(col, '{time}', to_jsonb(to_char(now(), 'YYYY-MM-DD')));
UPDATE 1
test# TABLE t;
col
════════════════════════
{"time": "2024-04-21"}
(1 row)