Re: Re: Rules, views, sequences and returned values

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "will trillich" <will(at)serensoft(dot)com>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Re: Rules, views, sequences and returned values
Date: 2001-03-23 17:23:35
Message-ID: 00a901c0b3bd$ff157100$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> > > I have a table and want to make a view updateable with rules. Table
has a
> > > sequence that creates default value to primary key when inserting.
> > >
> > > And now the question: Is it posible to return value of primary key
from
> > > rule of inserting with NEW? Is that value what i would get from
extern
> > > calling to insert, instead of OID?.
> >
> > SELECT currval('seq-table-name_seq-field-name_seq');
> >
> > where seq-table-name is the name of your table (not the view), and
> > seq-field-name is the name of the primary key.
>
> i'm sure there's a reason for this--
>
> psql=> select currval('mytable_afield_seq') ;
> ERROR: mytable_afield_seq.currval is not yet defined in this session
>
> but i'll be darned if i can figure out what it is. (after one
> call to "nextval()" currval() works fine, of course.) someone hit
> me with a clue stick!

currval() is the value last used by the backend, not by the database.
Meaning that you have to actually use the sequence before the backend has a
value to retrieve. That means either doing a nextval() or doing an INSERT
(which implicitly performs the nextval() ).

Greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tamsin 2001-03-23 17:34:34 RE: Alter column length
Previous Message Bruce Momjian 2001-03-23 17:12:02 Re: Slow SELECT...IN statements