{"id":1061,"date":"2013-03-08T07:52:42","date_gmt":"2013-03-08T07:52:42","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=471"},"modified":"2013-03-08T07:52:42","modified_gmt":"2013-03-08T07:52:42","slug":"magento-fix-integrity-constraint-violation-issue","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/magento-fix-integrity-constraint-violation-issue\/","title":{"rendered":"Magento – Fix integrity constraint violation issue"},"content":{"rendered":"

\"Database-banner\"<\/p>\n

Sometimes, you face database issues like this ‘integrity constraint violation: 1062 Duplicate entry ‘100000001’ for key’ . if you are not familiar with Magento, may be you will feel blind (like i was), debugging won’t go smoothly as usual.<\/p>\n

After studying Magento deeper to its core, i found that Magento numbering system depends on a table called ‘eav_entity_store’. this table stores increment ids of many entities from Magento.<\/p>\n

<\/p>\n

Back to our integrity constrant violation issues, we often have such troubles after upgrading Magneto to newer version. Mostly because of some increment ids are outdate. what we need to do to fix it is to update it to right number. for example, i my case, i have integrity constraint issue with creditmemo. i found that creditmemo have entity_type_id = 7 so i need to update increment_last_id from eav_entity_store table to what i found in sale_flat_creditmemo. This is my SQL script<\/p>\n

\n\nDELETE FROM magento_eav_entity_store\nWHERE\u00a0 entity_type_id = 7;\nINSERT INTO `magento_eav_entity_store` (entity_type_id,store_id,increment_prefix,increment_last_id)\nSELECT '7'\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AS entity_type_id,\nstore_id\u00a0\u00a0\u00a0\u00a0 AS store_id,\nstore_id\u00a0\u00a0\u00a0\u00a0 AS increment_prefix,\nincrement_id AS increment_last_id\nFROM\u00a0\u00a0 (SELECT *\nFROM\u00a0\u00a0 magento_sales_flat_creditmemo\nORDER\u00a0 BY increment_id DESC) AS temp GROUP\u00a0 BY store_id;\n\n<\/pre>\n

note : i have magento_ as table prefix, you can modify it to fit your case<\/p>\n

As you can see, i deleted all record that have entity_type_id = 7 (creditmemo entity id), then i created new row with data from sale_flat_creditmemo table. And that’s all \ud83d\ude00<\/p>\n

i hope you find this post helpful for your trouble.<\/p>\n","protected":false},"excerpt":{"rendered":"

Sometimes, you face database issues like this ‘integrity constraint violation: 1062 Duplicate entry ‘100000001’ for key’ . if you are not familiar with Magento, may be you will feel blind (like i was), debugging won’t go smoothly as usual. After studying Magento deeper to its core, i found that Magento numbering system depends on a…<\/p>\n

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,60,45],"tags":[49,59,61,62,51],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1061"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=1061"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1061\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=1061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=1061"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=1061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}