Re: Using a serial primary key as a foreign key in a second table

From: <operationsengineer1(at)yahoo(dot)com>
To: Nathaniel <naptrel(at)yahoo(dot)co(dot)uk>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Using a serial primary key as a foreign key in a second table
Date: 2006-12-21 20:39:42
Message-ID: 20061221203942.46268.qmail@web33313.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Subject: Re: [NOVICE] Using a serial primary key as a foreign key in a second table

When I want to add an "object", I add a row to one of the three child tables (depending
on the type of object I'm adding) and then add a record to the parent class table...

---------------------------------------------------

Nathaniel,

is there some kind of constraint that absolutely requires you to enter the child record before the parent record?

the customary way to do this is to add (or select) the parent, get its unique id, add the child(ren) and insert the parent's id into the child's foreign key -> done. this is pretty simple and straightforward once you get the code nomenclature down.

as Richard pointed out, once you start entering the child first, things get more complex. i'd recommend evaluating whether the "enter the child record first" constraint is really a required constraint. if not, drop it, enter the parent first, the child second and bask in the simplicity of the solution.

if a "child first" constraint is absolutely required the, well, you gotta do what you gotta do. just make sure it *is* actually required before going through the hassle of "doing what ya gotta do."

good luck.

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Browse pgsql-novice by date

  From Date Subject
Next Message Patrick 2006-12-24 04:38:05 Re: Using a serial primary key as a foreign key in a second
Previous Message Bruno Wolff III 2006-12-21 17:46:13 Re: Using a serial primary key as a foreign key in a second table