Re: Self-referential records

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Self-referential records
Date: 2010-01-24 14:24:59
Message-ID: hjhl7l$e5i$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ovid wrote on 24.01.2010 14:43:
> Assuming I have the following table:
>
> CREATE TABLE refers (
> id SERIAL PRIMARY KEY,
> name VARCHAR(255) NOT NULL,
> parent_id INTEGER NOT NULL,
> FOREIGN KEY (parent_id) REFERENCES refers(id)
> );
> I need to insert two records so that "select * from refers" looks like this:
>
> =# select * from refers;
> id | name | parent_id
> ----+------+-----------
> 1 | xxxx | 1
> 2 | yyy | 2
>
> The first record can't be inserted because I don't yet know the parent_id.

I ususally identify the root record by setting the parent_id to NULL.
In my experience creating a cycle in the tree creates a lot of trouble that is hard to come by.

Thomas

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andre Lopes 2010-01-24 14:27:29 How to use PG_DUMP?
Previous Message Bill Moran 2010-01-24 14:18:19 Re: Self-referential records