Unique constraint violation on Magento 1.4.0 to 1.6.2.0 upgrade - magento

I'm running an upgrade on an existing Magento site. After about 10 minutes, Magento reports an exception, and when I check the error report file in /var/report I see the following error message and stack dump:
a:5:{i:0;s:223:"Error in file: "/var/www/vhosts/mymagesite/app/code/core/Mage/Customer/sql/customer_setup/mysql4-upgrade-1.5.9.9-1.6.0.0.php" - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0-8' for key 'UNQ_BY_CUSTOMER'";i:1;s:952:"#0 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/Resource/Setup.php(645): Mage::exception('Mage_Core', 'Error in file: ...')
#1 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/Resource/Setup.php(437): Mage_Core_Model_Resource_Setup->_modifyResourceDb('upgrade', '1.4.0.0.7', '1.6.1.0')
#2 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/Resource/Setup.php(320): Mage_Core_Model_Resource_Setup->_upgradeResourceDb('1.4.0.0.7', '1.6.1.0')
#3 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/Resource/Setup.php(235): Mage_Core_Model_Resource_Setup->applyUpdates()
#4 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/App.php(412): Mage_Core_Model_Resource_Setup::applyAllUpdates()
#5 /var/www/vhosts/mymagesite/app/code/core/Mage/Core/Model/App.php(338): Mage_Core_Model_App->_initModules()
#6 /var/www/vhosts/mymagesite/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#7 /var/www/vhosts/mymagesite/index.php(80): Mage::run('default', 'store')
#8 {main}";s:3:"url";s:16:"/index.php/admin";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}
The general advice elsewhere on the Internet is to change <initStatements> in app/etc/config.xml to read:
<initStatements>SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;</initStatements>
However, disabling your databases integrity constraint system is a guaranteed path to incredibly difficult to support and troubleshoot issues later. It is a hack that makes the upgrade script not crash with an error, it does NOT actually fix the problem in any way shape or form.
Can the StackOverflow community assist with either a better solution, or a good explanation as to why disabling integrity checking in MySQL is a good idea?

This table is ok to truncate.
http://docs.nexcess.net/magento-database-maintenance
Its one of a handfull of tables gathering site usage information which are not critical to the operation of magento. (It does effect the customer reports, if your using those.)
The problem is with the migration script from:
/app/code/core/Mage/Customer/sql/customer_setup/mysql4-upgrade-1.5.9.9-1.6.0.0.php
Its altering a column which used to default to NULL to default to not null.
ALTER TABLE `report_compared_product_index` MODIFY COLUMN `customer_id` int UNSIGNED NOT NULL COMMENT ''
The error is from a unique index on that column. It was null before so MySQL was ignoring the unique index. Once it was set to default to not null, NULL is no longer a valid value and it attempts to set the value of the column to 0. It gets to the second row and now its breaking the unique index and you get an error,
http://bugs.mysql.com/bug.php?id=8173
The 1.4x code saved data in this table which isn't compatible with the new schema.
Its also going to be pretty hard to clean up because your missing information needed to satisfy the unique index.
The quickest option is to just truncate the table.

This is relatively easy to decipher from the error message. You have a duplicate customer (or more than one duplicate customers).
Open up your customer_entity table in phpMyadmin and look for duplicates. Depending on how many customers you have you may want to go through this manually, chances are that the duplicates will be from your own testing with 'fred#test.com' style emails. You should be able to safely delete these once you have gone through the table and worked out for yourself what has gone on.

Related

Can't update a product data with Magento

I'm having a problem in updating a product data.
The strange thing is that this problem is evidently connected to websites to which this product is assigned to. For some reason if the product is assigned to website A, I can't update the product's data - the "save" action is not executed.
But for website B, I can modify all the product's data. But if I try to assign a product to website A, I have the same problem and saving is not done. I compared the data for many websites and I can't spot anything that could be the cause for this.
I'm using Magento CE 1.4.2.0. Upgrade to the latest version is in progress, but I would still like to know what caused this.
The type of catalogrule_product_prices primary key is int(10). So when all keys were used up, the last valid key is 4294967295, see Numeric Datatypes. Try to change it to BIGINT and everything should work again.
I also noticed that I can't apply the catalog rules anymore - I got the error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4294967295' for key 'PRIMARY'
Luckily we had a backup copy of the system and there everything worked ok. As I compared the code I couldn't spot any difference that could be the cause of this problem, so I took a look in data base. As soon as I copied the data tables used for catalog rules (catalogrule, catalogrule_product, catalogrule_group_website, catalogrule_product_price) from the working backup copy and place it into the broken one, everything started working ok - the product saving and applying of the catalog rules. I still don't know how this happened and will have to do a data table comparison and maybe get my answer there.

Unable to save the invoice

After upgrading magento from 1.4.0.1 to 1.5.1.0, i'm not able to submit an invoice.
When i'm pointing my browser to Sales -> Orders -> [order] View -> Invoice and click Submit invoice, i'm getting the error saying Unable to save the invoice.
I've checked what's going on in my exception.log file and here is the error:
exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '100000001' for key 'UNQ_INCREMENT_ID'' in /home/misbhv/domains/*/public_html/lib/Zend/Db/Statement/Pdo.php:234
Stack trace:
#0 /home/misbhv/domains/*/public_html/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/misbhv/domains/*/public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 /home/misbhv/domains/*/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `sk...', Array)
#3 /home/misbhv/domains/*/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(337): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `sk...', Array)
#4 /home/misbhv/domains/*/public_html/lib/Zend/Db/Adapter/Abstract.php(574): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `sk...', Array)
#5 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(414): Zend_Db_Adapter_Abstract->insert('sklep_sales_fla...', Array)
#6 /home/misbhv/domains/*/public_html/app/code/core/Mage/Sales/Model/Mysql4/Order/Abstract.php(417): Mage_Core_Model_Mysql4_Abstract->save(Object(Mage_Sales_Model_Order_Invoice))
#7 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Sales_Model_Mysql4_Order_Abstract->save(Object(Mage_Sales_Model_Order_Invoice))
#8 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Model/Resource/Transaction.php(150): Mage_Core_Model_Abstract->save()
#9 /home/misbhv/domains/*/public_html/app/code/core/Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php(272): Mage_Core_Model_Resource_Transaction->save()
#10 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(420): Mage_Adminhtml_Sales_Order_InvoiceController->saveAction()
#11 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch('save')
#12 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#13 /home/misbhv/domains/*/public_html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
#14 /home/misbhv/domains/*/public_html/app/Mage.php(627): Mage_Core_Model_App->run(Array)
#15 /home/misbhv/domains/*/public_html/index.php(80): Mage::run('', 'store')
#16 {main}
I have no idea what to do with that and 'm not able to google any solution. Any help would be much appreciated!
The solution from: http://www.magentocommerce.com/boards/v/viewthread/234665/#t335407 makes sense:
i had the same error code. the increment number and the last order
number are not synchrone anymore. what we did is the following:
go to admin->sales-> orders and look up the highest order number (for
each store view!)
after some help from a fooman post, we did this: \"Then look at your
database. In the table eav_entity_type you will find all entity types
listed. The one of interest to change is where the order number
starts, ie. order sales/order. Remember the entity_type_id.\” (in my
install it is 4)
\"Next go to the table eav_entity_store. Look up the entity_type_id.
Now you can change the value of increment_last_id to your last actual
order number. (That is, if you wanted to have your next orderId to be
15000 set increment_last_id to 14999.)\”
Hope that this helps.
I actually looked at the solution above and thought it might work, you're on the right track but I tried something different:
Changing the 'increment_last_id' field solved no problem straight away - there was actually a set of orders existing from a previous store view that was deleted that shared the same order ID's so when Magento was trying to write the ID it failed in a duplicate error.
I found the problem by looking at my exception.log (it actually told me the order ID that was causing the problem).
Deleting those orders and re-aligning the new orders via editing the increment_last_id field solved my problem. Now my new orders start from the number above where they were last time and no order ID's clash anymore.
i think that you have a new restriction to unique in a row that in version 1.4.x don´t have required. You should see the duplicate row and solve create a new row with all the cascade constrains with another diferent increment_id.
You can see duplicate rows with the next sql statement:
SELECT row FROM table GROUP BY row HAVING COUNT(*)>1;
later you need see the duplicate row, in my case i can remove the duplicate jey because those are a errors. is probaby that you can remove the rows duplicate because this is injured by an error in you Magento.
In my case the magento scrip update stoped in whet try update the structure of the table.
I had an issue with the Fooman plugin and a blank space before all the increment_id's. This query fixed it for me ..
UPDATE `sales_flat_invoice` SET increment_id = trim(increment_id);
UPDATE `sales_flat_order` SET increment_id = trim(increment_id);
UPDATE `sales_flat_invoice_grid` SET increment_id = trim(increment_id);
UPDATE `sales_flat_order_grid` SET increment_id = trim(increment_id);
UPDATE `sales_flat_shipment` SET increment_id = trim(increment_id);
UPDATE `sales_flat_creditmemo` SET increment_id = trim(increment_id);
UPDATE `sales_flat_shipment_grid` SET increment_id = trim(increment_id);
UPDATE `sales_flat_creditmemo_grid` SET increment_id = trim(increment_id);
UPDATE `eav_entity_store` SET increment_last_id = trim(increment_last_id);

"There was a problem with reindexing process." after product import

Using Magento 1.6
I had to make some bulk changes to my catlog and so did a full product export, made the changes then imported the ammended file.
Afterwards there were a few index that needed updating, all of them except the "Product Attributes" index correctly.
When I try to re index that one I get the error "There was a problem with reindexing process."
There are no new errors created in var/report and so I have no idea what the problem is.
csv -> http://lazytrek.com/magento_export.csv
edit - As per OSdave's suggestion I got the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '84-142-1-58' for key 'PRIMARY'
After struggling for hours I finally found a solution. In fact, It's a very simple solution:
Backup your database
Open phpMyAdmin and truncate (empty) the table catalog_product_flat_1
That's it. After that I was able to index all data and, until now, everything else works perfectly. This worked for me. I hope it works for you too.
PS: using Magento 1.6.0.0
OK I fixed this myself
In the SQL error the first number (in my case 84) indicates the product id, something did not import properly with that ID, I went into the product, saved it manually and hey presto I could re-index.
Hope that helps someone :-)
Remove the lock files in var/locks and try again.
I'd advise you reindex in SSH if you have a large data set
php shell/indexer.php -reindexall
Source :: https://magento.stackexchange.com/questions/24729/there-was-a-problem-with-reindexing-process
I also had this issue and was getting the following in my exception log:
Integrity constraint violation: 1062 Duplicate entry '706-168-2-60' for key 'PRIMARY''
Eventually I discovered (as posted above) that this was a problem with product ID 706 (the first digits before '-' are the product ID.
Simply opening this product within the Magento admin and saving it fixed the issue with this product, however, in my case I also had a problem with product 707, 708, 709, etc, etc...
What I then discovered is the second set of digits identify the attribute ID. I figured out which attribute this was by opening any attribute and replacing it's ID in the URL with the one in the exception log.
I then searched for a series of products (in my case 700 to 800), selected them all and used "Actions" "Update attributes" in the top right of the Catalog - Manage Products page.
I amended this attribute for all of them (which was fine for me) and the indexer worked.
If you can't set this attribute to be the same for all your products I'd suggest a bulk import to reset just this.
Last night, I was having the same problem. After following the steps outlined by OSdave to get the more precise error message, I saw my error was the same as yours.
But it wasn't just with 1 product there were several hundred products causing errors (each one saved brought up a new one). And it wasn't all with the same attribute (I'd updated multiple attributes across a database of over 4,000 products in my last import).
Since everything seemed to actually be in the database correctly (since re-saving seemed to be fixing it and giving the proper data to the final saved product), I had an idea.
Why not export all 4k products, and then re-import the same file without modification and see if that clears it.
It worked!
tl;dr: If you're having this problem with multiple products needing to be re-saved, export your whole inventory (or the relevant section if you can segment in a useful way), and re-import the same file with no modifications.
I'm using Magento v 1.9.0.1
Thanks OSdave,
It's same error for me, I changed this
in Mage_Index_Adminhtml_ProcessController edit line 138, from Mage::helper('index')->__('Cannot initialize the indexer process.') to $e->getMessage():
then tried re-index, it's shows the below error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8308-2018-1-2788' for key 'PRIMARY', query was: INSERT INTO catalog_product_index_eav_idx (entity_id,attribute_id,store_id,value
then, I remove the product with ID '8308' and recreated and after tried to re-index, it working fine.
But don't the exact root cause of issue. I hope it's help to someone!
Double check the catalog_product_entity,catalog_product_entity_datetime,catalog_product_decimal,catalog_product_int, catalog_product_text,catalog_product_varchar table columns have with respective UNIQUE KEY's.

magento, error when reindexing Catalog URL Rewrites

All this afte rupgrading from 1.4.1
I get a quite specific error message:
Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'product/19-1-2' for key 'UNQ_CORE_URL_REWRITE_ID_PATH_IS_SYSTEM_STORE_ID'' in /home/in-due/domains/sb2.in-due.de/public_html/hochzeitsshop/lib/Zend/Db/Statement/Pdo.php:234
problem is, that I don't know how to find the entry, that is making trouble. neither in the backend (catalog|URL Rewrite Management) nor in the db itself (table core_url_rewrite) is an entry
product/19-1-2
any help is appreciated,
PAT ERLER
#perler, here is a MySQL query to detect duplicate SKUs. Remember, if you prefixed your tables, change catalog_product_entity accordingly!
SELECT
DISTINCT(`sku`) as `sku`,
COUNT(`sku`) as `skuCount`,
entity_id
FROM
catalog_product_entity
GROUP BY
`sku`
HAVING
`skuCount` > 1;
The duplicate entry is for a key made up from 3 fields, id_path, is_system and store_id, in theory you should be able to use URL Rewrite Management, search for an id_path of product/19 and this is your 'troublesome' field. Deleting that value may solve the issue (but I'd recommend making a backup first, and trying it in a dev environment first).
If you are still in development and haven't used any custom rewrites you could just empty the core_url_rewrite table and re-index to regenerate all the fields. If it is a live site this would be a little more troublesome, it would mean losing a bit of SEO juice if you have renamed products, since you would lose the rewrite.
Ok, the problem was a duplicate SKU. You shouldn't be able to enter one, but what happened (in magento 1.4.0.1) was, that the client entered a very long SKU which got shortened when saving the product. the shortened part was then identically to another SKU.
so, if yourself have this problem, check you database for duplicate SKUs (can someone post an SQL Query to do this?)

Magento - sales_flat_quote `reserved_id` not being released for some customers

EDIT:
It appears that under some circumstances (which I have yet to determine) the reserved_id field is not being reset/cleared for some customers. Therefore, when a very small percentage of customers log in after performing a successful transaction their quote_id is being assigned the value of that in the sales_flat_quote table.
Therefore when they try to place the order we are seeing integrity constraint violation errors in the logs - such as:
"SQLSTATE[23000]: Integrity constraint violation 1062 Duplicate entry "
This is beginning to look like a core bug but Its completely un-reproducible at the moment.
Magento version 1.5.0.1
Have you tried the database repair tool? It can be found at http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/db-repair-tool. It seems like yo need to repair your database.

Resources