Re: Is this a bug? Sequences and rules

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Is this a bug? Sequences and rules
Date: 2007-04-11 06:18:17
Message-ID: 20070411061817.GC29744@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Tue, dem 10.04.2007, um 22:49:27 -0700 mailte Chris Travers folgendes:
> Hi;
>
> I noticed that rules were not behaving properly. I created a test case,
> and it looks like the sequence is getting double-incrimented. Is this
> the way this is supposed to work?
>
> I know triggers would be better for something like this but I find these
> results... surprising....
>
> Version is 8.1.4
>
> postgres=# create table test1 (id serial, test text);
> NOTICE: CREATE TABLE will create implicit sequence "test1_id_seq" for
> serial column "test1.id"
> CREATE TABLE
> postgres=# create table test2 (id int);
> CREATE TABLE
> postgres=# create rule insert as on insert to test1 do also insert into
> test2 (id) values (new.id);
> CREATE RULE

Use this instead:
create rule insert as on insert to test1 do also insert into test2 (id) values (currval('test1_id_seq'));

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Travers 2007-04-11 06:24:50 The rule question before, request official documentation on the problem
Previous Message Tommy Gildseth 2007-04-11 06:14:31 Re: Is this a bug? Sequences and rules