IntegrityError duplicate key value violates unique constraint - django/postgres

人盡茶涼 提交于 2019-11-26 23:47:28
Hacking Life

This happend to me - it turns out you need to resync your primary key fields in Postgres. The key is the SQL statement:

SELECT setval('tablename_id_seq', (SELECT MAX(id) FROM tablename)+1)

It appears to be a known difference of behaviour between the MySQL and SQLite (they update the next available primary key even when inserting an object with an explicit id) backends, and other backends like Postgres, Oracle, ... (they do not).

There is a ticket describing the same issue. Even though it was closed as invalid, it provides a hint that there is a Django management command to update the next available key.

To display the SQL updating all next ids for the application MyApp:

python manage.py sqlsequencereset MyApp

In order to have the statement executed, you can provide it as the input for the dbshell management command. For bash, you could type:

python manage.py sqlsequencereset MyApp | python manage.py dbshell

The advantage of the management commands is that abstracts away the underlying DB backend, so it will work even if later migrating to a different backend.

I had the same issue. I had an existing table in my "inventory" app and I wanted to add new records in django admin and I got these messages:

Duplicate key value violates unique constraint "inventory_part_pkey" DETAIL: Key (part_id)=(1) already exists.

As mentioned before run the code bellow to get generate an SQL command to reset the id-s:

python manage.py sqlsequencereset inventory

In my case python manage.py sqlsequencereset MyApp | python manage.py dbshell was not working

  • So I copied the generated SQL statement.
  • Then opened pgAdmin for postgreSQL and opened my db.
  • Clicked on the 6. icon (Execute arbitrary SQL queries)
  • Copied the statement what was generated.

In my case it was:

BEGIN; SELECT setval(pg_get_serial_sequence('"inventory_signup"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "inventory_signup"; SELECT setval(pg_get_serial_sequence('"inventory_supplier"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "inventory_supplier"; COMMIT;

Executed it with F5.

This fixed all my tables and finally adding new records to the end not trying to add it to id = 1 any more.

In addition to zapphods answer:

In my case the indexing was indeed incorrect, since I had deleted all migrations, and the database probably 10-15 times when developing as I wasn't in the stage of migrating anything.

I was getting an IntegrityError on finished_product_template_finishedproduct_pkey

Reindex the table and restart runserver:

I was using pgadmin3 and for whichever index was incorrect and throwing duplicate key errors I navigated to the constraints and reindexed.

And then reindexed.

The solution is that you need to resync your primary key fields as reported by "Hacking Life" who wrote an example SQL code but, as suggested by "Ad N" is better to run the Django command sqlsequencereset to get the exact SQL code that you can copy and past or run with another command.

As a further improvement to these answers I would suggest to you and other reader to dont' copy and paste the SQL code but, more safely, to execute the SQL query generated by sqlsequencereset from within your python code in this way (using the default database):

from django.core.management.color import no_style
from django.db import connection

from myapps.models import MyModel1, MyModel2


sequence_sql = connection.ops.sequence_reset_sql(no_style(), [MyModel1, MyModel2])
with connection.cursor() as cursor:
    for sql in sequence_sql:
        cursor.execute(sql)

I tested this code with Python3.6, Django 2.0 and PostgreSQL 10.

If you have manually copied the databases, you may be running into the issue described here.

I encountered this error because I was passing extra arguments to the save method in the wrong way.

For anybody who encounters this, try forcing UPDATE with:

instance_name.save(..., force_update=True)

If you get an error that you cannot pass force_insert and force_update at the same time, you're probably passing some custom arguments the wrong way, like I did.

If you want to reset the PK on all of your tables, like me, you can use the PostgreSQL recommended way:

SELECT 'SELECT SETVAL(' ||
       quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
       quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
FROM pg_class AS S,
     pg_depend AS D,
     pg_class AS T,
     pg_attribute AS C,
     pg_tables AS PGT
WHERE S.relkind = 'S'
    AND S.oid = D.objid
    AND D.refobjid = T.oid
    AND D.refobjid = C.attrelid
    AND D.refobjsubid = C.attnum
    AND T.relname = PGT.tablename
ORDER BY S.relname;

After running this query, you will need to execute the results of the query. I typically copy and paste into Notepad. Then I find and replace "SELECT with SELECT and ;" with ;. I copy and paste into pgAdmin III and run the query. It resets all of the tables in the database. More "professional" instructions are provided at the link above.

I was getting the same error as the OP.

I had created some Django models, created a Postgres table based on the models, and added some rows to the Postgres table via Django Admin. Then I fiddled with some of the columns in the models (changing around ForeignKeys, etc.) but had forgotten to migrate the changes.

Running the migration commands solved my problem, which makes sense given the SQL answers above.

To see what changes would be applied, without actually applying them:
python manage.py makemigrations --dry-run --verbosity 3

If you're happy with those changes, then run:
python manage.py makemigrations

Then run:
python manage.py migrate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!