添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I am currently on Building RESTful APIs with Django RESET Framework lesson 15 Saving Objects.

After updating my ProductSerializer fields to:
fields = [‘id’, ‘title’, ‘description’, ‘slug’, ‘inventory’, ‘unit_price’, ‘price_with_tax’, ‘collection’]

And when I go to http://127.0.0.1:8000/store/products/ and input the following object to the POST field:
“title”: “test”,
“slug”: “a”,
“inventory”: 1,
“unit_price”: 1,
“collection”: 1

It gives me the following error:
duplicate key value violates unique constraint “store_product_pkey”
DETAIL: Key (id)=(11) already exists.

Please help… I’m stuck :frowning:

There is nothing wrong with your django project.
You’re getting this error because of a problem in your mysql database.

You can run this SQL statement in your database to fix this:

ALTER TABLE products AUTO_INCREMENT= the last id number in your products table + 1
 BFX0.9:

ALTER TABLE products AUTO_INCREMENT= the last id number in your products table + 1

I am using PostgreSQL, not mySQL. You think this is the reason why? Thanks!

This is the code for PostgreSQL:

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

here is the full discussion on StackOverFlow about this issue in Django and PostgreSQL:
https://stackoverflow.com/questions/11089850/integrityerror-duplicate-key-value-violates-unique-constraint-django-postgres

Sorry… One more question. When I tried the method that you suggested, it gives me the ERROR: relation “table name” does not exist

What should be the table name to use?
image1441×871 115 KB

Hello sorry for the late reply.
if you’re still having this problem.It’s cause instead of table name you need to write the actual name of your table. In this case store_product.
And for the first argument you pass in ‘store_product_id_seq’.

Thank you! I was having the same issue and your code helped me to solve that problem
I am using PostgreSQL and pgadmin4 and the following query helped to solve the issue
image1238×235 14.4 KB

POSTGRESQL15: I ran into the same issue. Due to whatever reason, after the import of the demo data, the internal id_sec counter for the table store_product was at 6, although 1000 records were inserted. As the field is a primary key (pk) field with auto-increment, one would expect, the internal counter id_sec is at 1000. But nope.

So first, we need to check the current value of the id_sec counter:
SELECT last_value FROM store_product_id_seq;
=> this gave me a result of 6 (and remember the Django error message:
"duplicate key value violates unique constraint “store_product_pkey”
DETAIL: Key (id)=(6) already exists.")

Seems we just found the culprit: obviously if the field id is a primary key, no double entries of the same value is allowed. Each record must have its own unique id number. We have 1000 records in the table, means ids from 1 to 1000 are already taken, so, if we insert a new record with the id of 6 it must obviously fail! And that’s the error message.

Next we need to find out where the counter should be by checking the highest value in the field ‘id’:
SELECT max(id) FROM store_product;
This returned a value of 1000. Of course, we have 1000 records that were inserted at once by the SQL script from Mosh.

And finally we need set the internal counter ‘id_sec’ to the correct value which is 1000:
SELECT setval(‘store_product_id_seq’, 1000, true);

Problem solved. Now try the code again and it will work.