错误:子查询必须只返回一列

时间:2016-03-18 07:06:57

标签: postgresql subquery postgresql-9.4

我正在尝试执行以下查询:

INSERT 
INTO rooms(
    id,
    name,
    body,
    parents,
    tags,
    createtime,
    creator,
    deletetime,
    meta,
    params,
    terms,
    updater,
    updatetime,
    counts,
    identities) 
SELECT *
FROM dblink ('dbname=oldsb',
    'SELECT '
        '(SELECT newid FROM id_map WHERE oldid = entities.id) AS id, '
        'id AS name, '
        'description AS body, '
        'NULL AS parents, '
        'NULL AS tags, '
        'ROUND(EXTRACT(EPOCH FROM current_timestamp)*1000) AS createtime, '
        'NULL AS creator, '
        'ROUND(EXTRACT(EPOCH FROM deletetime)*1000) AS deletetime, '
        'json_build_object(''picture'', picture) AS meta, '
        'jsonb_object_agg(
            (SELECT * '
            'FROM jsonb_each(params) '
            'AS fields (name, value) '
            'WHERE name <> ''places'')) AS params, '
        'terms AS terms, '
        'NULL AS updater, '
        'EXTRACT(EPOCH FROM lAStseentime)*1000 AS updatetime, '
        'NULL AS counts, '
        'NULL AS identities '
    'FROM entities WHERE type=''room''') 
AS t(
    id uuid,
    name text,
    body text,
    parents uuid[],
    tags smallint[],
    createtime bigint,
    creator text,
    deletetime bigint,
    meta jsonb,
    params jsonb,
    terms tsvector,
    updater text,
    updatetime bigint,
    counts jsonb,
    identities text[]);

我收到以下错误:

执行客房迁移查询 错误:子查询必须只返回一列 上下文:名为“unnamed”的dblink连接发生错误:无法执行查询。 用于更新房间身份的表格。

我无法理解查询出错的地方。

3 个答案:

答案 0 :(得分:0)

您的一个子查询返回多个结果。正是这个。

  

SELECT newid FROM id_map WHERE oldid = entities.id

将LIMIT 1添加到结尾并再试一次。实际上,您可以将LIMIT 1添加到每个子查询中,看看它是否有帮助。

答案 1 :(得分:0)

jsonb_each(params)有可能有多个列吗?

jsonb_object_agg(
        (SELECT * '
        'FROM jsonb_each(params) '
        'AS fields (name, value) '
        'WHERE name <> ''places'')) AS params,

我不知道jsonb_object_agg()的作用,但我不认为它是为了处理作为表的输入而设计的。通常,聚合函数将一个或多个结果合并为一个值。

答案 2 :(得分:0)

是的。查看this文档以获取json_each函数。 json_each和jsonb_each的工作方式相同。