Re: DEFERRABLE NOT NULL constraint

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: DEFERRABLE NOT NULL constraint
Date: 2013-02-07 11:05:46
Message-ID: kf01qa$dvn$1@gonzo.reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


here's a relatively clean way to do circular references:

given the circular reference:

table a (
i serial primary key ,
j integer references b(j) deferrable initially deferred
);

table b (
j serial primary key ,
i integer references a(i)
);

to make inserts easier put the default value of the column b.i
onto column a.j also (so both columns have the same sequence as their
default value)

then you ans do an INSERT INTO a [...] RETURNING i,j and have the
primary and foreign keys values needed for the new b row, without
needing to explictly reference the sequence in the query or
beforehand.

getting an ORM to follow that process may not be so easy, but is
probably the right thing to do.

--
⚂⚃ 100% natural

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2013-02-07 11:07:58 Re: REINDEX deadlock - Postgresql -9.1
Previous Message Anoop K 2013-02-07 11:00:02 Re: REINDEX deadlock - Postgresql -9.1