pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedProblem with SqlState=23505 when inserting rows
3+ messages / 3 participants
[nested] [flat]
* Problem with SqlState=23505 when inserting rows
@ 2020-01-15 16:40 Werner Kuhnle <wek@kuhnle.com>
2020-01-15 16:51 ` Re: Problem with SqlState=23505 when inserting rows Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-15 16:51 ` Re: Problem with SqlState=23505 when inserting rows Michael Lewis <mlewis@entrata.com>
0 siblings, 2 replies; 3+ messages in thread
From: Werner Kuhnle @ 2020-01-15 16:40 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
I'm new to pg and want to port an application and database from ms sql to pg v12.
Together with my application a database containing initial standard data
which is needed by the application is also installed.
This is done via execution of SQL scripts.
Every table of the db has an id column and id column values are also contained in the SQL script
which is necessary for consistence.
In MS SQL id columns were defined as
id INT IDENTITY PRIMARY KEY
In PG id columns are defined as
id SERIAL PRIMARY KEY
Importing the SQL script for initial standard data with PG Admin 4 works without any problem.
But afterwards I get error messages with SqlState=23505 when inserting new rows into that databases by my application.
My application does not provide id values when doing INSERT INTO statements so that new id values are automatically provided by the database.
This worked in MS SQL DB without any problems.
But in PG there seem to be conflicts between the row id values of the rows that were
initially imported and the row id values which are automatically provided by the database
if values for id column are missing in INSERT INTO
When automatically providing id values PG seems to ignore the already existing id values.
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.
Any hint is highly appreciated.
Regards
Werner
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Problem with SqlState=23505 when inserting rows
2020-01-15 16:40 Problem with SqlState=23505 when inserting rows Werner Kuhnle <wek@kuhnle.com>
@ 2020-01-15 16:51 ` Adrian Klaver <adrian.klaver@aklaver.com>
1 sibling, 0 replies; 3+ messages in thread
From: Adrian Klaver @ 2020-01-15 16:51 UTC (permalink / raw)
To: Werner Kuhnle <wek@kuhnle.com>; pgsql-general@lists.postgresql.org
On 1/15/20 8:40 AM, Werner Kuhnle wrote:
>
>
> Hi,
>
> I'm new to pg and want to port an application and database from ms sql to pg v12.
>
> Together with my application a database containing initial standard data
> which is needed by the application is also installed.
> This is done via execution of SQL scripts.
>
> Every table of the db has an id column and id column values are also contained in the SQL script
> which is necessary for consistence.
>
> In MS SQL id columns were defined as
> id INT IDENTITY PRIMARY KEY
>
> In PG id columns are defined as
> id SERIAL PRIMARY KEY
>
> Importing the SQL script for initial standard data with PG Admin 4 works without any problem.
>
> But afterwards I get error messages with SqlState=23505 when inserting new rows into that databases by my application.
> My application does not provide id values when doing INSERT INTO statements so that new id values are automatically provided by the database.
>
> This worked in MS SQL DB without any problems.
>
> But in PG there seem to be conflicts between the row id values of the rows that were
> initially imported and the row id values which are automatically provided by the database
> if values for id column are missing in INSERT INTO
> When automatically providing id values PG seems to ignore the already existing id values.
>
> 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.
>
> Any hint is highly appreciated.
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. To verify:
1) In psql do \d some_table and look for the sequence name in the
default column e.g:
production=# \d order_header
Table
"public.order_header"
Column | Type | Collation | Nullable |
Default
--------------+--------------------------------+-----------+----------+------------------------------------------------------
order_no | integer | | not null |
nextval('order_header_order_no_seq'::text::regclass)
So 'order_header_order_no_seq'
2) Then:
production=# select * from order_header_order_no_seq;
last_value | log_cnt | is_called
------------+---------+-----------
252 | 0 | t
(1 row)
3) Then in your case:
select max(id) from some_table
Postgres also has UPSERT:
https://www.postgresql.org/docs/12/sql-insert.html
Look for
ON CONFLICT Clause
Though I don't think this is what you need at this point.
>
> Regards
>
> Werner
>
>
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Problem with SqlState=23505 when inserting rows
2020-01-15 16:40 Problem with SqlState=23505 when inserting rows Werner Kuhnle <wek@kuhnle.com>
@ 2020-01-15 16:51 ` Michael Lewis <mlewis@entrata.com>
1 sibling, 0 replies; 3+ messages in thread
From: Michael Lewis @ 2020-01-15 16:51 UTC (permalink / raw)
To: Werner Kuhnle <wek@kuhnle.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
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 )
);
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2020-01-15 16:51 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-01-15 16:40 Problem with SqlState=23505 when inserting rows Werner Kuhnle <wek@kuhnle.com>
2020-01-15 16:51 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-15 16:51 ` Michael Lewis <mlewis@entrata.com>
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