Re: SERIAL type not autoincremented

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: teknet(at)poczta(dot)onet(dot)pl
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: SERIAL type not autoincremented
Date: 2004-07-02 20:36:23
Message-ID: 20040702203623.GA3839@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Fri, Jul 02, 2004 at 21:40:02 +0200,
teknet(at)poczta(dot)onet(dot)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.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Paul Gimpelj 2004-07-02 20:58:11 [admin] 32mb box 1gb
Previous Message Mike Rylander 2004-07-02 20:32:02 Re: SERIAL type not autoincremented