Serial field and using a rule on insert - Mailing list pgsql-sql

From brad
Subject Serial field and using a rule on insert
Date
Msg-id 36AE5D8E.B4EF2808@kieser.net
Whole thread Raw
List 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

Attachment

pgsql-sql by date:

Previous
From: "Oliver Elphick"
Date:
Subject: Re: [SQL] Referencial integrity
Next
From: "񊀔"
Date:
Subject: subscribe pgsql-admin