Skip to content

Instantly share code, notes, and snippets.

View awright415's full-sized avatar

Adam Wright awright415

View GitHub Profile
@awright415
awright415 / graph_preload.ex
Created June 27, 2021 16:23
Recursively Preloading Children from Join Table w/ Elixir & Ecto
# Use a recursive CTE to turn the join table into a (partial) closure table,
# then join all of the child records for the provided parent(s)
query = """
WITH RECURSIVE traversal(depth, parent_id, child_id) AS (
SELECT
1 depth,
parent_id,
child_id
FROM rules_rules
WHERE parent_id = ANY($1)