Postgres CTE:非递归项中的类型字符变化(255)[]但整体上类型字符变化[]

时间:2012-09-19 03:50:57

标签: postgresql graph-theory common-table-expression

我是SO和postgres的新手所以请原谅我的无知。尝试使用与此帖Find cluster given node in PostgreSQL

类似的解决方案在postgres中获取图表的集群

唯一的区别是我的id是UUID,我使用varchar(255)来存储这个id

当我尝试运行查询时,我收到以下错误(但不确定如何投射):

ERROR: recursive query "search_graph" column 1 has type character varying(255)[] in non-recursive term but type character varying[] overall

SQL状态:42804     提示:将非递归项的输出转换为正确的类型。 性格:81

我的代码(与上一篇文章基本相同):

WITH RECURSIVE search_graph(path, last_profile1, last_profile2) AS (
SELECT ARRAY[id], id, id
FROM node WHERE id = '408d6b12-d03e-42c2-a2a7-066b3c060a0b'
UNION ALL
SELECT sg.path || m.toid || m.fromid, m.fromid, m.toid
FROM search_graph sg
JOIN rel m
ON (m.fromid = sg.last_profile2 AND NOT sg.path @> ARRAY[m.toid]) 
   OR (m.toid = sg.last_profile1 AND NOT sg.path @> ARRAY[m.fromid])
)

 SELECT DISTINCT unnest(path) FROM search_graph;

1 个答案:

答案 0 :(得分:7)

尝试将递归和非递归字词中的SELECT列表转换为varchar

WITH RECURSIVE search_graph(path, last_profile1, last_profile2) AS (
    SELECT ARRAY[id]::varchar[], id::varchar, id::varchar
    FROM node WHERE id = '408d6b12-d03e-42c2-a2a7-066b3c060a0b'
  UNION ALL
    SELECT (sg.path || m.toid || m.fromid)::varchar[], m.fromid::varchar, m.toid::varchar
    FROM search_graph sg
    JOIN rel m
    ON (m.fromid = sg.last_profile2 AND NOT sg.path @> ARRAY[m.toid]) 
       OR (m.toid = sg.last_profile1 AND NOT sg.path @> ARRAY[m.fromid])
)
SELECT DISTINCT unnest(path) FROM search_graph;