Duplicate key value violates unique constraint

Started by Nafees Sardarabout 6 years ago3 messagesbugs
Jump to latest
#1Nafees Sardar
nafees@epazz.net

Hi support,

I migrate my data from MS Sql server to postgreSQL database, now when I try
to insert new row it gives me error "duplicate key value violates unique
constraint". For example I have a table which contains 100 rows of records
when I go for inserting 101 row it give duplicate value constraint error.
When I pass PK value manually its working, and one more thing auto increment
is enabled for all tables .

Can you please let me know anything I am missing? I will really appreciate
it.

Thanks

Regards

Nafees Sardar

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Nafees Sardar (#1)
Re: Duplicate key value violates unique constraint

On Monday, March 16, 2020, Nafees Sardar <nafees@epazz.net> wrote:

Hi support,

I migrate my data from MS Sql server to postgreSQL database, now when I
try to insert new row it gives me error “duplicate key value violates
unique constraint”. For example I have a table which contains 100 rows of
records when I go for inserting 101 row it give duplicate value constraint
error. When I pass PK value manually its working, and one more thing auto
increment is enabled for all tables .

Can you please let me know anything I am missing? I will really
appreciate it.

ALTER SEQUENCE

David J.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Nafees Sardar (#1)
Re: Duplicate key value violates unique constraint

"Nafees Sardar" <nafees@epazz.net> writes:

I migrate my data from MS Sql server to postgreSQL database, now when I try
to insert new row it gives me error "duplicate key value violates unique
constraint". For example I have a table which contains 100 rows of records
when I go for inserting 101 row it give duplicate value constraint error.
When I pass PK value manually its working, and one more thing auto increment
is enabled for all tables .

Sounds like you need to adjust the sequence counters underlying the
auto-increment columns so that their settings are higher than any
of the already-existing ID values. Auto-increment isn't really a
native concept in Postgres, it's something you have to build from
a sequence object and a suitable default expression. Getting the
sequence set correctly is an easy step to miss when migrating from
a different DBMS.

regards, tom lane