Re: 7.4: serial not working ?

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: vertigo <none(at)microsoft(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: 7.4: serial not working ?
Date: 2004-07-08 17:23:01
Message-ID: 1089307381.29095.16.camel@braydb
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, 2004-07-02 at 11:24, vertigo wrote:
> Hello
> i upgraded postgresql from 7.3 to 7.4 and noticed that SERIAL
> exists but does not working. Example:
> My table:
> create table1(
> id SERIAL,
> name VARCHAR(100)
> );
>
> insert into table1 (name) values('name1');
> ERROR: duplicate key violates unique constraint "table1_pkey"
>
> Why ? I want to have autoincrementation. I do not know (when i insert
> record) what id values should it have.
> How can i solve this problem ?

You already have rows in that table, and the sequence is generating a
key which already exists. This is not MySQL!

To cure this, set the sequence to the highest value in the table and
don't insert explicit values for that column:

SELECT setval('table1_id_seq', (SELECT MAX(id) FROM table1));

(or something like that).

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Ted Kremenek 2004-07-08 17:50:48 Re: [CHECKER] 4 memory leaks in Postgresql 7.4.2
Previous Message Martin Pitt 2004-07-08 15:01:23 Re: Fwd: postgresql-autodoc - missinterprets "version"