1

I have a Magento 2.2.3 installation and I have the theme SM-Maxshop on that. I am not able to add any products, categories or Megamenu items to it. I get an error "Unique constraint violation found" when I do any of these operations. I was able to add custom product attributes and add them to attribute sets too, but am stuck after that. Has anyone else faced any issue like this?

PFA screenshots of the error pages. Add new category in Magento error Add new product in Magento 2 Add menu items Magento 2.2.3

I see this in the exception.log file:

I see this entry in exception.log:

[2018-05-14 05:36:06] main.CRITICAL: Unique constraint violation found {"exception":"[object] (Magento\Framework\Exception\AlreadyExistsException(code: 0): Unique constraint violation found at /home/.../public_html/.../vendor/magento/framework/EntityManager/Operation/Create.php:134, Magento\Framework\DB\Adapter\DuplicateException(code: 1062): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY', query was: INSERT INTO url_rewrite (redirect_type,is_autogenerated,metadata,description,store_id,entity_type,entity_id,request_path,target_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at /home/.../public_html/.../vendor/magento/framework/DB/Adapter/Pdo/Mysql.php:555, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY', query was: INSERT INTO url_rewrite (redirect_type,is_autogenerated,metadata,description,store_id,entity_type,entity_id,request_path,target_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at /home/.../public_html/.../vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:235, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' at /home/.../public_html/.../vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228)"} []

  • Please check system.log and paste relevant info here. – Vivek Kumar May 14 '18 at 5:24
  • I did not see anything relevant in system.log. But pasted the error from exception.log – Priyanka May 14 '18 at 5:42
  • How many products do you have in your db? – Vivek Kumar May 14 '18 at 5:49
  • Only 6 right now. I have added custom attributes and created attribute sets though. – Priyanka May 14 '18 at 6:06
0

You should TRUNCATE tables catalog_url_rewrite_product_category and URL_rewrite and you can recreate the required url keys by just resaving all the categories.

I hope this will resolve your problem. Just in case keep a backup of your tables.

EDIT:

After looking at your complete stack trace, I am pretty sure some module is running a oberver/plugin/preference at product and category save at setting url_key to 0 value.It will give error as url_key should be unique for every product and category.

Please check this and update.

  • I tried this, but it did not work. I still get the same error and exception.log also has same entry :( – Priyanka May 14 '18 at 6:43
  • Please check edit – Vivek Kumar May 14 '18 at 7:55
0

SOLVED Invoice Unique Constraint Violation Magento 2.2.5 when i am generating a invoice facing this error

Just go to data base -> sales_invoice -> indexes just remove one rule "SALES_INVOICE_INCREMENT_ID_STORE_ID '

  • Removing indexes is a very bad idea. It will allow you to create multiple invoices with the same increment id which can later on break other processes, such as payment integrations. – Alex Dinca Dec 17 '18 at 12:56
0

This is most likely an issue with a primary key field. Make sure that auto increment is present for such a field. If it's missing, you can simply add it by running SQL queries:

SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE your_table_name MODIFY your_field_name INTEGER NOT NULL AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS=1;

Also, you can change the autoincrement value and set it bigger, so the next created item will have a higher ID.

In my case, I have had the error "Unique constraint violation found" in Magento 2 Blog extension by Magefan on one of my projects when I tried to add a new blog post and save it. For some reason, table magefan_blog_post has not had increment setting for post ID. I fixed this by running this SQL queries:

SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE magefan_blog_post MODIFY post_id INTEGER NOT NULL AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS=1;

The strange is that on other Magento instance there was no issue, so I think that this is some glitches with the Magento installation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.