pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: brad <brad@kieser.net>
To: pgsql-sql@postgresql.org
Subject: Serial field and using a rule on insert
Date: Wed, 27 Jan 1999 00:27:59 +0000
Message-ID: <36AE5D8E.B4EF2808@kieser.net> (raw)

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

Attachments:

brad.vcf (text/x-vcard, 206B) download begin:vcard n:Kieser;Bradley x-mozilla-html:TRUE org:Kieser Consultancy Limited adr:;;;;;; version:2.1 email;internet:brad@kieser.demon.co.uk title:Director x-mozilla-cpt:;18656 fn:Bradley Kieser end:vcard
smime.p7s (application/x-pkcs7-signature, 2.1K) download

Message-ID: <36AE5D8E.B4EF2808@kieser.net>
Permalink:  ../36AE5D8E.B4EF2808@kieser.net/
Also on:    postgresql.org/message-id/36AE5D8E.B4EF2808@kieser.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: brad@kieser.net
  Subject: Re: Serial field and using a rule on insert
  In-Reply-To: <36AE5D8E.B4EF2808@kieser.net>
  References: <36AE5D8E.B4EF2808@kieser.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox