2 weeks, 3 days ago Mike Reiche

How to get the depth of the graph?

WITH RECURSIVE graph as (
    SELECT pr.*,@depth as depth FROM rcte_people_relationships pr WHERE source_id=3
  UNION
    SELECT pr.*,(@depth:=@depth+1) as depth FROM rcte_people_relationships pr, graph WHERE graph.target_id=pr.source_id
) 
SELECT * FROM graph
CROSS JOIN (SELECT @depth := 0) AS depth;

Creates an infinite loop.