Re: Using a trigger with an object-relational manager

From: Rick Schumeyer <rschumeyer(at)ieee(dot)org>
To: PFC <lists(at)peufeu(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Using a trigger with an object-relational manager
Date: 2007-05-23 12:41:00
Message-ID: 4654365C.1020208@ieee.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Actually, the situation is slightly more complicated. It's more like I
have tables A1, A2, and A3 each of which must have a corresponding row
in B. So each of A1, A2 and A3 has a BEFORE INSERT trigger that creates
a row in B and sets a FK in A1 (or A2 or A3). So I can't just use the
same PK in both the A tables and B.

PFC wrote:
>
>> I'm using Ruby on Rails and have two tables, A and B. Every row in A
>> needs a corresponding row in B. A also contains a FK pointing to B.
>>
>> I created a before insert trigger on A that inserts a new row in B,
>> and sets the FK in A. This seems to be running fine.
>
> So, A has a b_id field linking to B ?
>
> If you need a 1-1 relationship, you could try instead to use the
> same primary key in B than in A :
>
> A : id SERIAL PRIMARY KEY
> B : id INTEGER PRIMARY KEY (not SERIAL)
>
> Then, AFTER INSERT trigger on A checks the value the sequence put in
> A.id and inserts in B with this value as the PK.
>
> Postgres has INSERT ... RETURNING which is a very clean and
> elegant solution but Rails never heard about it...

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message PFC 2007-05-23 12:45:49 Re: Using a trigger with an object-relational manager
Previous Message Rick Schumeyer 2007-05-23 12:36:39 Re: Using a trigger with an object-relational manager