Re: Using a trigger with an object-relational manager

Поиск
Список
Период
Сортировка
От PFC
Тема Re: Using a trigger with an object-relational manager
Дата
Msg-id op.tsrnzmqxcigqcu@apollo13
обсуждение исходный текст
Ответ на Using a trigger with an object-relational manager  (Rick Schumeyer <rschumeyer@ieee.org>)
Ответы Re: Using a trigger with an object-relational manager  (Rick Schumeyer <rschumeyer@ieee.org>)
Список pgsql-general
> 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...




В списке pgsql-general по дате отправления:

Предыдущее
От: sudhir
Дата:
Сообщение: Re: Lock table, Select for update and Serialization error
Следующее
От: "Albe Laurenz"
Дата:
Сообщение: Re: Lock table, Select for update and Serialization error