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

From Mike Rylander
Subject Re: SERIAL type not autoincremented
Date
Msg-id cc4hm8$14d0$1@news.hub.org
Whole thread Raw
In response to Re: SERIAL type not autoincremented  (teknet@poczta.onet.pl)
List pgsql-admin
Larry Rosenman wrote:

> 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 ?
>>
>>
>>
>> Thanx
>>
>> Michal
>
> Because you didn't let the serial column do it's magic.
>
> Try:
>
> Insert into test2(name) values('myname');
> Insert into test2(name) values('myname2');
>
> That should work.
>
> Always let a serial column pick the number.

Or, if you must specify the column, specify DEFAULT for the value:

INSERT INTO test2 (id,name) values (DEFAULT,'myname3');

>
> LER
>
>


pgsql-admin by date:

Previous
From: Sandro Dentella
Date:
Subject: Re: pg_clog error after crash
Next
From:
Date:
Subject: Forcing postgres to reload current time zone.