Re: SERIAL type not autoincremented - Mailing list pgsql-admin

From Bruno Wolff III
Subject Re: SERIAL type not autoincremented
Date
Msg-id 20040702203623.GA3839@wolff.to
Whole thread Raw
In response to Re: SERIAL type not autoincremented  (teknet@poczta.onet.pl)
List pgsql-admin
On Fri, Jul 02, 2004 at 21:40:02 +0200,
  teknet@poczta.onet.pl wrote:
> i found the problem:
>
>  
>
> sys=> create table test2(
> sys(> id serial,
> sys(> name varchar(10),
> sys(> primary key(id)
> sys(> );
> NOTICE:  CREATE TABLE will create implicit sequence "test2_id_seq" for
> "serial" column "test2.id"
> NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test2_pkey"
> for table "test2"
> CREATE TABLE
> sys=> insert into test2 values(1,'myname');
> INSERT 18765 1
> sys=> insert into test2 (name) values('myname2');
> ERROR:  duplicate key violates unique constraint "test2_pkey"
> sys=>
>
> Why is it so ?

Because you are inserting records without using the sequence. The serial
type is really a short cut for specifying that the default value is
the value of a sequence created for that column. If you insert records
without using the default, then you also need to set the value of the
sequence higher than the largest value so far. You can use the setval
function to do this.

pgsql-admin by date:

Previous
From: Radu-Adrian Popescu
Date:
Subject: Re: SERIAL type not autoincremented
Next
From: "Larry Rosenman"
Date:
Subject: Re: SERIAL type not autoincremented