Serial field and using a rule on insert

From: brad <brad(at)kieser(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Serial field and using a rule on insert
Date: 1999-01-27 00:27:59
Message-ID: 36AE5D8E.B4EF2808@kieser.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 Content-Type Size
brad.vcf text/x-vcard 207 bytes

Browse pgsql-sql by date

  From Date Subject
Next Message hgati 1999-01-27 01:57:43 subscribe pgsql-admin
Previous Message Oliver Elphick 1999-01-26 22:38:24 Re: [SQL] Referencial integrity