Postgresql jsonb 列到行提取的值

Postgresql jsonb column to row extracted values

我有以下table

BEGIN;

CREATE TABLE IF NOT EXISTS "public".appevents (
    id uuid DEFAULT uuid_generate_v4() NOT NULL,
    "eventId" uuid NOT NULL,
    name text NOT NULL,
    "creationTime" timestamp without time zone NOT NULL,
    "creationTimeInMilliseconds" bigint NOT NULL,
    metadata jsonb NOT NULL,
    PRIMARY KEY(id)
);

COMMIT;

我想通过查询将 metadata jsonb 列提取为一行并尝试使用以下查询。

SELECT 
    userId
FROM 
    appevents, jsonb_to_record(appevents.metadata) as x(userId text)

不幸的是,userid 返回的所有行都具有 NULL 值,这是不正确的。注意到的唯一奇怪的事情是它正在将驼峰式大写字母转换为小写字母,但似乎不是问题所在。

这是我目前在 userId 存在的数据库中的 2 条记录。

The only weird thing noticed is that it is converting camelcase to lowercase but doesn't seem like the issue.

实际上这是罪魁祸首 - 默认情况下列名不区分大小写,并且 userId 被规范化为 userid,因此 JSON 不包含 属性。引用标识符 (… as x("userId" text)) 应该有效。

但是,有一个更简单的解决方案可以将 json 对象属性作为文本访问:->> 运算符。您可以使用

SELECT metadata->>'userId' AS userid FROM appevents