Re: [SQL] nextval

From: Jerome ALET <alet(at)unice(dot)fr>
To: pgsql-sql(at)postgresql(dot)org
Cc: patrice(at)idf(dot)net, tony(at)cys(dot)de
Subject: Re: [SQL] nextval
Date: 1998-05-28 08:39:44
Message-ID: 356D22D0.7EE91817@unice.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Patrice Hédé wrote:
>
> > CREATE SEQUENCE seq_slogans INCREMENT 1 MINVALUE 1 START 1;
> > CREATE
> >
> > CREATE RULE rule_i_slogans AS ON INSERT TO slogans DO UPDATE NEW SET
> > numero_slogan = nextval(seq_slogans) , points_slogan = 10;
> > ERROR: attribute 'seq_slogans' not found
>
> Maybe I've overlooked something, but you seem to have forgotten to put
> quotes around the sequence name as :
>
> SELECT nextval('seq_slogans');

OK, thanks to all.

I was wrong because I had forgotten the '' but it still doesn't work
correctly:
First I had to remove PRIMARY KEY and NOT NULL because when I did the
INSERT it complained about inserting a null value. This is because the
NOT NULL check is automatically done before the RULE is executed. Is it
a bug or a feature ? Maybe we should be able to choose the order but
this should not exist in ANSI SQL...
Second, look at the results of a similar RULE:

CREATE TABLE auteurs (numero_auteur INT4, nom_auteur TEXT, prenom_auteur
TEXT, email_auteur TEXT);

CREATE SEQUENCE seq_auteurs INCREMENT 1 MINVALUE 1 START 1;
CREATE

CREATE RULE rule_i_auteurs AS ON INSERT TO auteurs DO UPDATE NEW SET
numero_auteur = nextval('seq_auteurs');
CREATE

then:
slogbase=> insert into auteurs (nom_auteur) values ('Jerome');
UPDATE 1
slogbase=> select * from auteurs;
numero_auteur|nom_auteur|prenom_auteur|email_auteur
-------------+----------+-------------+------------
1|Jerome | |
(1 row)

this result is correct but when I continue:

slogbase=> insert into auteurs (nom_auteur) values ('Alfred');
UPDATE 2

I think, but I'm not sure, that now the inserted line has a
numero_auteur which value is 2 ! Let's verify:

slogbase=> select * from auteurs;
numero_auteur|nom_auteur|prenom_auteur|email_auteur
-------------+----------+-------------+------------
2|Jerome | |
3|Alfred | |
(2 rows)

another try:

slogbase=> insert into auteurs (nom_auteur) values ('Albert');
UPDATE 3
slogbase=> select * from auteurs;
numero_auteur|nom_auteur|prenom_auteur|email_auteur
-------------+----------+-------------+------------
4|Jerome | |
5|Alfred | |
6|Albert | |
(3 rows)

Interesting, isn't it ?

The 'AS ON INSERT' section of my rule seems to be interpreted like:
'AS ON (SELECT OR INSERT)'

Someone has got any idea ?

Jerome ALET - alet(at)unice(dot)fr - http://cortex.unice.fr/~jerome
Faculte de Medecine de Nice - http://noe.unice.fr - Tel: 04 93 37 76 30
28 Avenue de Valombrose - 06107 NICE Cedex 2 - FRANCE

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Patrice Hédé 1998-05-28 09:48:08 Re: [SQL] nextval
Previous Message Brian Capouch 1998-05-28 06:22:25 Grant/Revoke