2

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)"} []

5
  • Please check system.log and paste relevant info here. May 14, 2018 at 5:24
  • I did not see anything relevant in system.log. But pasted the error from exception.log
    – Priyanka
    May 14, 2018 at 5:42
  • How many products do you have in your db? May 14, 2018 at 5:49
  • Only 6 right now. I have added custom attributes and created attribute sets though.
    – Priyanka
    May 14, 2018 at 6:06
  • refer this link magento.stackexchange.com/questions/264083/… Dec 6, 2019 at 5:37

6 Answers 6

2

Probably your AUTO_INCREMENT is set wrong for the table. You can check this with:

SELECT 
`AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
AND   TABLE_NAME   = 'table_name';

You can then fix the auto increment by setting it the the highest ID +1

ALTER TABLE table_name AUTO_INCREMENT = start_value;
1

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.

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

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 '

1
  • 7
    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, 2018 at 12:56
1

Solved in Magento 2.4.3!

For me the problem was that the afterReplace function in Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage added duplicate url rewrites to the catalog_url_rewrite_product_category table. Since a while there is a primary key on that table so this gives an error.

The solution is to delete the existing url_rewrites first, the function removeMultiple already exists.

Overwrite Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage and add this line:

$this->productResource->removeMultiple(array_column($toSave, 'url_rewrite_id')); 

before the saveMultiple between lines 63 - 64.

So it becomes like this:

if (count($toSave) > 0) {
    $this->productResource->removeMultiple(array_column($toSave, 'url_rewrite_id'));
    $this->productResource->saveMultiple($toSave);
}
0

This might be relevant to this issue:

If you have a parent category with is_anchored = true and the product you are trying to remove from the parent exists on a child category this constraint will trigger.

I can't confirm if it exists in all cases, but we are using Elasticsearch which contains the catalog_category_product index and have a "Smart Category" module which handles catalog_category_product index on it's own and whenever a save happens it attempts to check up on url_rewrites. so in our case it might just be this module.

I can consistently make it work by attempting to remove the product from the parent category. however if i go to the child category and remove the product, the exception does not happen. afterwards i can then remove the product from the parent category

0

I also got same issue but i feel its magento Limit issue.

in my case i have around 4 level of category and each as many product when you save a category based on configuration it try to create a url rewrite entries for all product with parent, child category path which have more then 10000 entries to url rewrite table

vendor/magento/module-catalog-url-rewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php->execute()

their is a function called in side execute function doBunchReplace($productUrlRewriteResult) you try to send parameter 1000000 then i can save the category without any issue default value is 10000

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.