From: | Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> |
---|---|
To: | Janning Vygen <vygen(at)planwerk6(dot)de> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Problem with n to n relation |
Date: | 2001-10-09 15:38:30 |
Message-ID: | Pine.BSF.4.21.0110090829410.82151-100000@megazone23.bigpanda.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
> 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.
From | Date | Subject | |
---|---|---|---|
Next Message | Janning Vygen | 2001-10-09 15:55:56 | Re: Problem with n to n relation |
Previous Message | Herb Blacker | 2001-10-09 15:09:21 | Loading current_user and current_timestamp using COPY |