Thread: CHILD WITHOUT PARENT (TEMPORARILY)

CHILD WITHOUT PARENT (TEMPORARILY)

From
JORGE MALDONADO
Date:
I am developing an app in which there are 2 tables: a parent and a child. The difference is that, in my case, the child table is generated first and, if the user decides to keep the information then a record is generated in the parent table and the child table is connected to its parent. The field in the child table that connects to its parent is temporarily filled with a value of -1. The user who types the information may decide to keep or not the records in the child table and it is at this time when a record in the parent table is generated or not. Meanwhile, the records in the child table have a tamporary value of -1 in the field that will connect to its parent table and this causes me not to be able to define a foreign key.
Please advice.
 
Respectfully,
Jorge Maldonado

Re: CHILD WITHOUT PARENT (TEMPORARILY)

From
Richard Broersma
Date:
On Mon, Dec 28, 2009 at 2:58 PM, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:
>
 The field
> in the child table that connects to its parent is temporarily filled with a
> value of -1... this causes me not to be able to define a foreign key.

Rather than using -1, perhaps you should use null.  This would be a
valid for it.


--
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug

Re: CHILD WITHOUT PARENT (TEMPORARILY)

From
Jasen Betts
Date:
On 2009-12-28, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:

> I am developing an app in which there are 2 tables: a parent and a child.
> The difference is that, in my case, the child table is generated first and,
> if the user decides to keep the information then a record is generated in
> the parent table and the child table is connected to its parent. The field
> in the child table that connects to its parent is temporarily filled with a
> value of -1. The user who types the information may decide to keep or not
> the records in the child table and it is at this time when a record in the
> parent table is generated or not. Meanwhile, the records in the child table
> have a tamporary value of -1 in the field that will connect to its parent
> table and this causes me not to be able to define a foreign key.
> Please advice.

One fix is to use NULL instead of -1

Another is to make the connstraint DEFERRABLE INITIALLY DEFERRED and
create the child records inside a transaction not COMMITing the
tranaction until thwey have been UPDATEd with the correct foreign key
value,

A third is to create a record in the parent table with a key of -1