标签: postgresql-json

从 jsonb 成员更新整数列失败:列的类型为整数,但表达式的类型为 jsonb

在 PostgreSQL 9.5 表中,我有一integersocial

当我尝试在存储过程中更新它时,在in_userstype 变量中给出以下 JSON 数据(一个包含 2 个对象的数组,每个对象都有一个“社交”键)jsonb

'[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum,
Germany","female":0,"stamp":1450102770},
  {"sid":"54321284239407942","auth":"ddddc1808197a1161bc22dc307abbbbb",**"social":4**,"given":"Alxander2","family":"Farber","photo":null,"place":"Bochum,
Germany","female":0,"stamp":1450102800}]'::jsonb
Run Code Online (Sandbox Code Playgroud)

然后以下代码失败:

    FOR t IN SELECT * FROM JSONB_ARRAY_ELEMENTS(in_users)
    LOOP
            UPDATE words_social SET
                    social = t->'social',
            WHERE sid = t->>'sid';
    END LOOP;
Run Code Online (Sandbox Code Playgroud)

带有错误消息:

ERROR:  column "social" is of type integer but expression is of type jsonb
LINE 3:                         social = t->'social',
                                         ^
HINT:  You will need to rewrite or cast the expression.
Run Code Online (Sandbox Code Playgroud)

我曾尝试将该行更改为:

social = t->'social'::int,
Run Code Online (Sandbox Code Playgroud)

但后来我得到了错误:

ERROR:  invalid …
Run Code Online (Sandbox Code Playgroud)

postgresql json plpgsql postgresql-json postgresql-9.5

3
推荐指数
1
解决办法
1146
查看次数

在 WHERE ... IN 条件下使用 JSONB_ARRAY_ELEMENTS

在线扑克玩家可以选择购买游戏室 1 或游戏室 2 的使用权。

他们可以因作弊而被暂时禁止。

CREATE TABLE users (
        uid SERIAL PRIMARY KEY,

        paid1_until timestamptz NULL,     -- may play in room 1
        paid2_until timestamptz NULL,     -- may play in room 2

        banned_until timestamptz NULL,    -- punished for cheating etc.
        banned_reason varchar(255) NULL
);
Run Code Online (Sandbox Code Playgroud)

这里上表填充了4条测试记录:

INSERT INTO users (paid1_until, paid2_until, banned_until, banned_reason)
VALUES (NULL, NULL, NULL, NULL),
       (current_timestamp + interval '1 month', NULL, NULL, NULL),
       (current_timestamp + interval '2 month', current_timestamp + interval '4 month', NULL, NULL),
       (NULL, current_timestamp + …
Run Code Online (Sandbox Code Playgroud)

postgresql select where-in postgresql-json postgresql-9.5

3
推荐指数
1
解决办法
9869
查看次数

row_to_json 给出带有小写字段名称的 JSON

我创建了一个自定义类型如下:

create type my_type as (camelCasedIdentifier uuid, ...);
Run Code Online (Sandbox Code Playgroud)

我正在使用此自定义类型my_type来定义 JSON 正文中的字段名称:

select row_to_json(row(my_table.id, ...)::my_type) from my_table;
Run Code Online (Sandbox Code Playgroud)

我认为使用自定义类型很有用的原因是,通过这种方式,我不必在每个查询中定义 JSON 字段名称(在我的情况下它们与表列名称不同),因为您必须这样做json_build_object().

然而,这里的问题是字段名称现在全部为小写:

{"camelcasedidentifier":"d8f0a177-af13-4fa2-a2af-3bc8296d848e", ...}
Run Code Online (Sandbox Code Playgroud)

我期望:

{"camelCasedIdentifier":"d8f0a177-af13-4fa2-a2af-3bc8296d848e", ...}
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?我知道这可以通过使用来解决select json_build_object('camelCasedIdentifier', my_table.id) from my_table,但我宁愿不这样做,因为我将被迫在每个查询中枚举 JSON 字段名称。

postgresql json postgresql-json

2
推荐指数
1
解决办法
1431
查看次数

UPSERT测试代码中的语法错误

我正在尝试使用以下测试代码来测试新的PostgreSQL upsert语法,但是出现语法错误:

test=> CREATE TABLE test1 (
test(>         key1 integer PRIMARY KEY check (key1 > 0),
test(>         key2 integer check (key2 > 0)
test(> );
CREATE TABLE

test=> CREATE OR REPLACE FUNCTION upsert(IN in_json_array jsonb)
test->         RETURNS void AS
test-> $func$
test$>         UPDATE test1 t SET     
test$>         t.key1 = (obj->>'key1')::int,
test$>         t.key2 = (obj->>'key2')::int
test$>         FROM JSONB_ARRAY_ELEMENTS(in_json_array) obj
test$>         WHERE  t.key1 = obj->'key1'
test$>         ON CONFLICT DO UPDATE SET
test$>         key1 = excluded.key1,
test$>         key2 = excluded.key2;
test$> 
test$> …
Run Code Online (Sandbox Code Playgroud)

postgresql insert upsert postgresql-json postgresql-9.5

0
推荐指数
1
解决办法
1716
查看次数