On Dec 9, 2007 9:56 AM, Alain Roger <raf.news@gmail.com> wrote:
> Hi Tom,
>
> but when i let pgsql setup everything (i mean when i create table -> pgsql
> creates sequence)
> ), i have called = no, before using any select nextval()...
> and in this case, it works great.
>
> but once called = yes, select nextval(sequence_name); always gives me
> current value +1 :-(
The whole point of the serial type is that you don't have to call
nextval yourself.
smarlowe=# create table test (i serial primary key, info text);
NOTICE: CREATE TABLE will create implicit sequence "test_i_seq" for
serial column "test.i"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"test_pkey" for table "test"
CREATE TABLE
smarlowe=# insert into test (info) values ('this is a row');
INSERT 0 1
smarlowe=# select * from test;
i | info
---+---------------
1 | this is a row
If you need the current value to insert into another table, you use currval:
smarlowe=# select currval('test_i_seq');
currval
---------
1