Re: [SQL] nextval - Mailing list pgsql-sql

From Jerome ALET
Subject Re: [SQL] nextval
Date
Msg-id 356D22D0.7EE91817@unice.fr
Whole thread Raw
In response to Re: [SQL] nextval  (Patrice Hédé <patrice@idf.net>)
Responses Re: [SQL] nextval  (Patrice Hédé <patrice@idf.net>)
List 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@unice.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

pgsql-sql by date:

Previous
From: Brian Capouch
Date:
Subject: Grant/Revoke
Next
From: Patrice Hédé
Date:
Subject: Re: [SQL] nextval