check for circular references in a 2-table heirachy

From: Terence Kearns <terencek(at)isd(dot)canberra(dot)edu(dot)au>
To: pgsql-sql(at)postgresql(dot)org
Subject: check for circular references in a 2-table heirachy
Date: 2004-09-09 01:43:33
Message-ID: 413FB545.9090100@isd.canberra.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Does anyone know how I could check to ensure circular references are no
created in my 2 table heirachy structure?

Here are the tables

albums album_relations
+------------+ +-----------------+
| album_id | | parent_album_id |
| title | | child_album_id |
+------------+ +-----------------+

This structure allows an album to have multiple parents.
I have written a recursive function (on a PHP class) which creates an
XML tree structure based on a 2D array result set**. It has a depth
checker so you can set the limit of recursion.

This is all well and good, but I want to create a PLPG/SQL trigger on
the album_relations table to ensure that I won't have to throw the
"depth limit exceeded" exception due to circular references.

Any ideas would be appreciated.

** 2D array result set is built from
SELECT a.*,r.parent_album_id
FROM album_relations r
RIGHT JOIN albums a
ON a.album_id = r.child_album_id

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Swan 2004-09-09 01:58:24 Re: Isnumeric function?
Previous Message Theo Galanakis 2004-09-09 00:02:54 Re: Isnumeric function?