pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Werner Kuhnle <wek@kuhnle.com>
To: pgsql-general@lists.postgresql.org
Subject: Re-2: Problem with SqlState=23505 when inserting rows
Date: Thu, 16 Jan 2020 08:14:57 +0000
Message-ID: <DIIE.00000D9E0000CD8A@kuhnle.com> (raw)
In-Reply-To: <CAHOFxGoaDbQ3Ah5gEz4Dkj9eyvbVBxz+cWHprNoxxMvzbBSWQQ@mail.gmail.com>
References: <DIIE.00000B1F0000CD69@kuhnle.com>
	<CAHOFxGoaDbQ3Ah5gEz4Dkj9eyvbVBxz+cWHprNoxxMvzbBSWQQ@mail.gmail.com>



Thanks Michael und Adrian for your answers.


I think that
"
My guess is that the SERIAL was defined for the column, then the data 
was added with id values, but the counter for the sequence behind the 
SERIAL was not updated to a value greater the the last id added.
"
is a correct description of what happens.


For avoiding the need of an additional command for updating the sequence
before every insert statement:


Is there a way to specify the desired behaviour 
(that PG always provides conflict-free id values, eg. max(id)+1 when
id values are not given explicitly in the INSERT statement)
already whend defining(!) the table in the CREATE TABLE statement ?








Original Message processed by davidĀ® 
Re: Problem with SqlState=23505 when inserting rows 15. Januar 2020, 17:51 Uhr 
Von Michael Lewis 
An Werner Kuhnle 
Cc PostgreSQL General 









On Wed, Jan 15, 2020 at 9:41 AM Werner Kuhnle <wek@kuhnle.com> wrote:


I've tried to using the newer definition:
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
but that does not solve the problem.

Is there a way to define id columns to that when the database provides values,
it recognizes already existing values avoiding conflicts.





You'll need to run something like the below to set the next value to the max current value. You'll just have to figure out the name of the sequence that is automatically created whether you use the pseudo type serial, or the newer IDENTITY option. Both are implemented with a sequence.





--set sequence to max ID on a table


select setval( 'table_name_id_seq', ( select max(id) + 1 from table_name ) );

view thread (2+ messages)  latest in thread

Message-ID: <DIIE.00000D9E0000CD8A@kuhnle.com>
Permalink:  ../DIIE.00000D9E0000CD8A@kuhnle.com/
Also on:    postgresql.org/message-id/DIIE.00000D9E0000CD8A@kuhnle.com

 · 

reply

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-general@postgresql.org
  Cc: wek@kuhnle.com
  Subject: Re: Re-2: Problem with SqlState=23505 when inserting rows
  In-Reply-To: <DIIE.00000D9E0000CD8A@kuhnle.com>
  References: <DIIE.00000B1F0000CD69@kuhnle.com> <CAHOFxGoaDbQ3Ah5gEz4Dkj9eyvbVBxz+cWHprNoxxMvzbBSWQQ@mail.gmail.com> <DIIE.00000D9E0000CD8A@kuhnle.com>

* 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