Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Created November 8, 2017 09:03
Show Gist options
  • Select an option

  • Save alexesDev/efbc35b8ae0f667aecbde4608d83de23 to your computer and use it in GitHub Desktop.

Select an option

Save alexesDev/efbc35b8ae0f667aecbde4608d83de23 to your computer and use it in GitHub Desktop.
WITH RECURSIVE tree AS (
SELECT id, parent_path, ARRAY[]::INTEGER[] AS ancestors
FROM core.items
WHERE parent_id is null
UNION ALL
SELECT i.id, i.parent_path, tree.ancestors || i.parent_id
FROM core.items i, tree
WHERE i.parent_id = tree.id
)
update core.items i
set parent_path = array_to_string(array_prepend('root', ancestors::text[]), '.')::ltree
from tree
where tree.id = i.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment