Re: Circular references - Mailing list pgsql-general

From Alban Hertroys
Subject Re: Circular references
Date
Msg-id CAF-3MvMWktN2N5V7+MfO=AmuFycyk3Ookx57aoYULbiekdUg3g@mail.gmail.com
Whole thread Raw
In response to Re: Circular references  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On 21 June 2013 05:08, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Melvin Call <melvincall979@gmail.com> writes:
> I was given a dump of an existing remote schema and database, and the
> restore on my local system failed. Looking into it, I found a circular
> parent-child/child-parent relationship, and I don't believe this existing
> structure is viable. To summarize, the organization entity has an attribute
> of creator, which is a foreign key to the user table, but the user has to
> belong to an organization, which is a foreign key to the organization
> table. Since neither are nullable, there is no way to create even an
> initial record. My guess is one or both of the tables was first populated,
> and then the FK constraint(s) created.

That should be possible if you create one of the FK constraints as a DEFERRED constraint. Which one depepnds on what order you supply the data in.

And make sure you supply all the data in the circular reference in the same transaction, or the deferred constraint check will kick in too early.

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

pgsql-general by date:

Previous
From: Yuriy Rusinov
Date:
Subject: Re: WIN1251 localization
Next
From: Ladislav Lenart
Date:
Subject: Re: Circular references