Django Saving Objects

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:

1 Like

Hi
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

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

I would not recommend touch database directly at all. Check your views and models and templates. Your error comes because you are trying to add new object with same id which should be unique. I can not give you other suggestion unless I can see a code

1 Like

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?

I’ve dowloaded Mosh’s code and still ran into the same problem… :frowning:

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’.

1 Like

This is the exact code for PostgreSQL:

select setval(‘store_product_id_seq’, (select max(id) from store_product) + 1);

1 Like

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

1 Like

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.

The +1 shouldn’t be needed. The internal id_sec counter should be identical to the highest value of id as otherwise you will leave one out with the next insert operation.