pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Janning Vygen <vygen@planwerk6.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: Problem with n to n relation
Date: Tue, 9 Oct 2001 08:38:30 -0700 (PDT)
Message-ID: <Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <0110090942060D.30615@janning>

> Yes you are right! It doesnt work. i thought i have tested it...
> 
> But i dont understand your solution. Why did you insert foo in the 
> person table? Dou want to use it as a foreign key? Maybe you just 
> mistyped your alter table statement i guess. you create a unique 
> person2address id and references foo to it. So 'foo' will always 
> refernce the first address inserted. Right? Ok thats a fine solution, 
> but there is more work to do like a trigger when deleting this 
> address... 

Yeah, miscopied the statement.  And you're right, I'd forgotten about
delete.  I think you'd probably be better off faking the check constraint
in a deferred constraint trigger.

> Is this the preferable solution?? I am wondering about tutorials 
> never explaining stuff like that. Is it too uncommon to have a person 
> with at least one address?
Well, the *best* way (that doesn't work in postgres) is probably
to have a check constraint with a subselect, something like
check exists(select * from person2address where ...) initially
deferred.  But we don't support subselect in check directly, and its not
likely to happen soon (it's a potentially very complicated constraint).

There are locking issues, but one could probably use a constraint trigger
(a postgres specific thing I think, but...) and have the trigger do a
select * from person2address where... and raise an exception if no
matches are found. The locking issues are due to the fact that you
could run into problems with multiple backends trying to do stuff to 
the same rows if you're not careful, although I think it might work
out with for update.




view thread (9+ messages)  latest in thread

Message-ID: <Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com>
Permalink:  ../Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: sszabo@megazone23.bigpanda.com, vygen@planwerk6.de
  Subject: Re: Problem with n to n relation
  In-Reply-To: <Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com>
  References: <0110090942060D.30615@janning> <Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox