postgresql duplicate key violates unique constraint

前端 未结 8 927
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 10:37

Hi i have a question i know this was posted many times but i didn\'t find an answer to my problem. The problem is that i have a table and a column \"id\" i want it to be uni

相关标签:
8条回答
  • 2020-12-02 10:53

    For future searchs, use ON CONFLICT DO NOTHING.

    0 讨论(0)
  • 2020-12-02 10:56

    Intro

    I also encountered this problem and the solution proposed by @adamo was basically the right solution. However, I had to invest a lot of time in the details, which is why I am now writing a new answer in order to save this time for others.

    Case

    My case was as follows: There was a table that was filled with data using an app. Now a new entry had to be inserted manually via SQL. After that the sequence was out of sync and no more records could be inserted via the app.

    Solution

    As mentioned in the answer from @adamo, the sequence must be synchronized manually. For this purpose the name of the sequence is needed. For Postgres, the name of the sequence can be determined with the command PG_GET_SERIAL_SEQUENCE. Most examples use lower case table names. In my case the tables were created by an ORM middleware (like Hibernate or Entity Framework Core etc.) and their names all started with a capital letter.

    In an e-mail from 2004 (link) I got the right hint.

    (Let's assume for all examples, that Foo is the table's name and Foo_id the related column.)

    Command to get the sequence name:

    SELECT PG_GET_SERIAL_SEQUENCE('"Foo"', 'Foo_id');
    

    So, the table name must be in double quotes, surrounded by single quotes.

    1. Validate, that the sequence is out-of-sync

    SELECT CURRVAL(PG_GET_SERIAL_SEQUENCE('"Foo"', 'Foo_id')) AS "Current Value", MAX("Foo_id") AS "Max Value" FROM "Foo";
    

    When the Current Value is less than Max Value, your sequence is out-of-sync.

    2. Correction

    SELECT SETVAL((SELECT PG_GET_SERIAL_SEQUENCE('"Foo"', 'Foo_id')), (SELECT (MAX("Foo_id") + 1) FROM "Foo"), FALSE);
    
    0 讨论(0)
  • 2020-12-02 10:59

    I have similar problem but I solved it by removing all the foreign key in my Postgresql

    0 讨论(0)
  • 2020-12-02 11:00

    On the first place, I thought the problem was the sequence :). But the error says ERROR: duplicate key value violates unique constraint "ndxregnum".. So I look for the name "ndxregnum" from my database. I found it under my table indexes. Then I delete "ndxregnum". Then error is gone. everything back to normal.

    I hope this will help you...

    0 讨论(0)
  • 2020-12-02 11:08

    From http://www.postgresql.org/docs/current/interactive/datatype.html

    Note: Prior to PostgreSQL 7.3, serial implied UNIQUE. This is no longer automatic. If you wish a serial column to be in a unique constraint or a primary key, it must now be specified, same as with any other data type.

    0 讨论(0)
  • 2020-12-02 11:08

    Referrence - https://www.calazan.com/how-to-reset-the-primary-key-sequence-in-postgresql-with-django/

    I had the same problem try this: python manage.py sqlsequencereset table_name

    Eg:

    python manage.py sqlsequencereset auth
    

    you need to run this in production settings(if you have) and you need Postgres installed to run this on the server

    0 讨论(0)
提交回复
热议问题