| От | brad |
|---|---|
| Тема | Serial field and using a rule on insert |
| Дата | |
| Msg-id | 36AE5D8E.B4EF2808@kieser.net обсуждение исходный текст |
| Список | pgsql-sql |
I have two tables, one is a master table ("visitors") and the other is a
detail table ("visits").
FYI, here are the definitions:
create table visitors (id serial, site text, first_visit datetime,
last_visit datetime);
create table visits (id int4, visit_time datetime);
I want to have a rule that inserts a row into "visits" when I insert
into "visitors", such as the following code:
CREATE RULE record_visit_time_ins AS ON INSERT
TO visitors DO
INSERT into visits (id, visit_time) VALUES
(NEW.id, datetime('NOW'::abstime));
The problem is that I need to record "id" in the detail table's insert.
"Id" is a serial type in the master table (it is a sequential number).
If I use "OLD" in the insert, the field is blank as this is an insert,
but if I use "NEW", the value inserted into the detail table increments!
For example, if the new value for "id" in table "visitors" is 1, then
the value inserted for "id" in "visits" is 2, not 1. I assume that this
is because of some quirk using the "NEW" qualifier.
All that I want to do is record the id and a timestamp in "visits" for
the row inserted in "visitors".
Can anyone help?
Thanks,
Brad
В списке pgsql-sql по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера