From: Bruno Wolff III <bruno@wolff.to>
To: Hortschitz, Stefan <Stefan.Hortschitz@lfrz.at>
Cc: pgsql-admin@postgresql.org
Subject: Re: failed insert into serial-type row
Date: Sat, 18 Jun 2005 09:06:10 -0500
Message-ID: <20050618140610.GA15547@wolff.to> (raw)
In-Reply-To: <D12153701D57FF45A1AD64DFB6571D7C79DCCD@svie0107.vie01.local>
References: <D12153701D57FF45A1AD64DFB6571D7C79DCCD@svie0107.vie01.local>
On Mon, Jun 13, 2005 at 21:58:37 +0200,
"Hortschitz, Stefan" <Stefan.Hortschitz@lfrz.at> wrote:
>
> CREATE TABLE session.preferences
> (
> id serial NOT NULL,
> name varchar(50),
> value varchar(255),
> CONSTRAINT session_preferences_pkey PRIMARY KEY (id)
> )
> WITHOUT OIDS;
> ALTER TABLE session.preferences OWNER TO web;
> --------------------------------
>
> and sometimes if i'm adding new rows to the table with statements like
> this:
> insert into (name,value) ('xy','xy');
> the database only returns an 'duplicate key on primary key', which
> should be prevented by the serial-datatype, or? how could it be, that
> two rows become the same id??
> if i execute the insert-statement again it works.
>
> the created serial-sequence looks like this:
> --------------------------------
> CREATE SEQUENCE session.preferences_id_seq
> INCREMENT 1
> MINVALUE 1
> MAXVALUE 9223372036854775807
> START 2487
> CACHE 1;
> ALTER TABLE session.preferences_id_seq OWNER TO web;
> --------------------------------
>
> what can I do against the duplicate-key problem? is it a configuration,
> sql or operating-system specific problem?
Most likely you recreated the table and reloaded it at some point without
restoring the sequence value.
What you want to do now is set the sequence to the highest value of id
in the table. Something like:
SELECT setval('preferences_id_seq',(SELECT max(id) FROM session.preferences);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: bruno@wolff.to, Stefan.Hortschitz@lfrz.at
Subject: Re: failed insert into serial-type row
In-Reply-To: <20050618140610.GA15547@wolff.to>
References: <D12153701D57FF45A1AD64DFB6571D7C79DCCD@svie0107.vie01.local> <20050618140610.GA15547@wolff.to>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox