471,049 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

self referencing table structure and constraints

I have a categories table that contains a FK to another category in the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0

There is no record with category_id 0 because ROOT is at the top of the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?
I thought of pointing ROOT to itself, but since we have some
recursive code that starts at a given category id and moves up the tree
it will hit the ROOT category and loop forever. I'd like to fix this
by constraining the database so that even working from psql it would be
difficult to damage this table by hand.
Are there any widely used techniques for dealing with this type of
constraint?
Thanks,
-M@
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 2331

On Thu, 23 Sep 2004, Matthew Hixson wrote:
I have a categories table that contains a FK to another category in the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0
There is no record with category_id 0 because ROOT is at the top of the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?
Well, to simply have the root category not error, you could use NULL for
the parent_id if you're using a foreign key. However, it sounds like your
full problem is more complicated.

If you want to force that there always exists exactly 1 such row, it's
harder. Forcing that there's no more than 1 should be possible without
writing triggers (maybe a unique index on ((1)) where parent_id is null)
but I'm not sure how else to guarantee that there's at least 1 besides a
trigger.
I thought of pointing ROOT to itself, but since we have some
recursive code that starts at a given category id and moves up the tree
it will hit the ROOT category and loop forever. I'd like to fix this
by constraining the database so that even working from psql it would be
difficult to damage this table by hand.


Well, in that case you also may need to watch out for cycles. You can do
this with triggers, but handling concurrent changes might get tricky.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
On Sep 23, 2004, at 6:36 PM, Stephan Szabo wrote:

On Thu, 23 Sep 2004, Matthew Hixson wrote:
I have a categories table that contains a FK to another category in
the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0


There is no record with category_id 0 because ROOT is at the top of
the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete
a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?


Well, to simply have the root category not error, you could use NULL
for
the parent_id if you're using a foreign key.


Okay, now I just feel silly. For some reason I was thinking that the
parent id couldn't be NULL either.
Thanks, this is exactly what I needed.
-M@
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by toedipper | last post: by
4 posts views Thread by maricel | last post: by
10 posts views Thread by Macka | last post: by
1 post views Thread by Andrew Banks | last post: by
reply views Thread by Doug Gault | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.