使用pgAdmin 4时,bigint不正确地插入JSONB列

问题描述 投票:0回答:1

我在插入具有bigint字段的jsonb值时观察到奇怪的行为。根据doc jsonb支持数值数据类型所以它不应该是一个问题。

表:

CREATE TABLE document_wrapper
(
    id integer NOT NULL,
    document jsonb NOT NULL,
    CONSTRAINT document_pkey PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

样品插页:

insert into document_wrapper(id, document) values(-8, '{"id":101861191707868275}');

现在查询文档时:

SELECT document FROM document_wrapper;

给出结果(结尾注0):

{ "id": 101861191707868270 }

但是当我从中选择实际的id值时,在每种情况下都是正确的:

SELECT
    (document->'id')::text, 
    jsonb_extract_path_text(d.document, 'id') , 
    (document #>> '{id}')::bigint, 
    (document->>'id')::numeric, 
    (document->'id')::bigint
FROM document_wrapper d 
WHERE id = -8 ;

每种情况下结果为101861191707868275。

为什么在json中可见的值与在那里首先被插入的那个不同?这会导致将json发送到后端应用程序时出现问题,该应用程序的值不正确。

Postgres版本:x86_64-pc-linux-gnu上的PostgreSQL 11.2,由gcc(GCC)4.8.5 20150623(Red Hat 4.8.5-36)编译,64位

更新:代码通过psql工具正确运行。问题发生在pgAdmin和应用程序(驱动程序https://mvnrepository.com/artifact/org.postgresql/postgresql版本42.2.5)

postgresql pgadmin
1个回答
0
投票

所以问题实际上是java脚本。问题仅出现在Web应用程序中(webapp pg_admin,应用程序前端)。传递的数字扩展了js-es Number.MAX_SAFE_INTEGER,导致数字被舍入。愚蠢的我相信后端会根据浏览器中的响应(已经四舍五入)返回错误的数据。

作为解决方法,我已将数字更改为字符串

© www.soinside.com 2019 - 2024. All rights reserved.