Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Wright awright415

  • San Francisco, CA
View GitHub Profile
@awright415
awright415 / graph_preload.ex
Created Jun 27, 2021
Recursively Preloading Children from Join Table w/ Elixir & Ecto
View graph_preload.ex
# 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)