Re: Using a serial primary key as a foreign key in a second table - Mailing list pgsql-novice

From
Subject Re: Using a serial primary key as a foreign key in a second table
Date
Msg-id 20061221203942.46268.qmail@web33313.mail.mud.yahoo.com
Whole thread Raw
In response to Using a serial primary key as a foreign key in a second table  (Nathaniel <naptrel@yahoo.co.uk>)
List 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'sid into the child's foreign key -> done.  this is pretty simple and straightforward once you get the code
nomenclaturedown. 

as Richard pointed out, once you start entering the child first, things get more complex.  i'd recommend evaluating
whetherthe "enter the child record first" constraint is really a required constraint.  if not, drop it, enter the
parentfirst, 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*
actuallyrequired 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

pgsql-novice by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Using a serial primary key as a foreign key in a second table
Next
From: Patrick
Date:
Subject: Re: Using a serial primary key as a foreign key in a second