Magento类别产品reindex错误

时间:2015-04-16 11:43:55

标签: php database magento

我最近将magento从1.5升级到1.9,除了重新编制类别产品之外,一切正常。

在管理员后端,我只得到一个错误,即进程失败。

所以,我是通过命令行使用shell/indexer.php完成的,这是输出:

[www-data@DevServer shell]$ php -f indexer.php reindexall
Product Attributes index was rebuilt successfully in 00:00:20
Product Prices index was rebuilt successfully in 00:00:06
Catalog URL Rewrites index was rebuilt successfully in 00:02:59
Category Products index process unknown error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dev_salveo`.`catalog_category_product_index`, CONSTRAINT `FK_CAT_CTGR_PRD_IDX_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID` FOREIGN KEY (`category_id`) REFERENCES `catalog_category_entity` (`entity_id`) ON )' in /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/www-data/dev.my-domain.co.uk/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#5 /home/www-data/dev.my-domain.co.uk/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)
#6 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(179): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...')
#7 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Helper/Mysql4.php(49): Mage_Index_Model_Resource_Abstract->insertFromSelect(Object(Varien_Db_Select), 'catalog_categor...', Array, false)
#8 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(153): Mage_Index_Model_Resource_Helper_Mysql4->insertData(Object(Mage_Catalog_Model_Resource_Category_Indexer_Product), Object(Varien_Db_Select), 'catalog_categor...', Array, false)
#9 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(113): Mage_Index_Model_Resource_Abstract->insertFromTable('catalog_categor...', 'catalog_categor...', false)
#10 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Catalog/Model/Resource/Category/Indexer/Product.php(935): Mage_Index_Model_Resource_Abstract->syncData()
#11 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Indexer/Abstract.php(143): Mage_Catalog_Model_Resource_Category_Indexer_Product->reindexAll()
#12 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Process.php(212): Mage_Index_Model_Indexer_Abstract->reindexAll()
#13 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Process.php(260): Mage_Index_Model_Process->reindexAll()
#14 /home/www-data/dev.my-domain.co.uk/public_html/shell/indexer.php(167): Mage_Index_Model_Process->reindexEverything()
#15 /home/www-data/dev.my-domain.co.uk/public_html/shell/indexer.php(215): Mage_Shell_Compiler->run()
#16 {main}

Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dev_salveo`.`catalog_category_product_index`, CONSTRAINT `FK_CAT_CTGR_PRD_IDX_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID` FOREIGN KEY (`category_id`) REFERENCES `catalog_category_entity` (`entity_id`) ON ), query was: INSERT INTO `catalog_category_product_index` (`category_id`, `product_id`, `position`, `is_parent`, `store_id`, `visibility`) SELECT `catalog_category_product_index_idx`.`category_id`, `catalog_category_product_index_idx`.`product_id`, `catalog_category_product_index_idx`.`position`, `catalog_category_product_index_idx`.`is_parent`, `catalog_category_product_index_idx`.`store_id`, `catalog_category_product_index_idx`.`visibility` FROM `catalog_category_product_index_idx` ON DUPLICATE KEY UPDATE `category_id` = VALUES(`category_id`), `product_id` = VALUES(`product_id`), `position` = VALUES(`position`), `is_parent` = VALUES(`is_parent`), `store_id` = VALUES(`store_id`), `visibility` = VALUES(`visibility`)' in /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Statement/Pdo.php:235
Stack trace:
#0 /home/www-data/dev.my-domain.co.uk/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /home/www-data/dev.my-domain.co.uk/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /home/www-data/dev.my-domain.co.uk/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)
#5 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(179): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...')
#6 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Helper/Mysql4.php(49): Mage_Index_Model_Resource_Abstract->insertFromSelect(Object(Varien_Db_Select), 'catalog_categor...', Array, false)
#7 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(153): Mage_Index_Model_Resource_Helper_Mysql4->insertData(Object(Mage_Catalog_Model_Resource_Category_Indexer_Product), Object(Varien_Db_Select), 'catalog_categor...', Array, false)
#8 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Resource/Abstract.php(113): Mage_Index_Model_Resource_Abstract->insertFromTable('catalog_categor...', 'catalog_categor...', false)
#9 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Catalog/Model/Resource/Category/Indexer/Product.php(935): Mage_Index_Model_Resource_Abstract->syncData()
#10 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Indexer/Abstract.php(143): Mage_Catalog_Model_Resource_Category_Indexer_Product->reindexAll()
#11 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Process.php(212): Mage_Index_Model_Indexer_Abstract->reindexAll()
#12 /home/www-data/dev.my-domain.co.uk/public_html/app/code/core/Mage/Index/Model/Process.php(260): Mage_Index_Model_Process->reindexAll()
#13 /home/www-data/dev.my-domain.co.uk/public_html/shell/indexer.php(167): Mage_Index_Model_Process->reindexEverything()
#14 /home/www-data/dev.my-domain.co.uk/public_html/shell/indexer.php(215): Mage_Shell_Compiler->run()
#15 {main}
Catalog Search Index index was rebuilt successfully in 00:00:24
Stock Status index was rebuilt successfully in 00:00:00
Tag Aggregation Data index was rebuilt successfully in 00:00:00

知道这个错误意味着什么吗?我该如何解决?

1 个答案:

答案 0 :(得分:7)

此错误是因为实体表中缺少类别ID或产品ID。 尝试运行Category Products索引时,由于索引过程尝试在catalog_category_product_index表中插入行,而product_id或category_id不存在。这导致“Intergrity Constraint Violation”。

当从较低版本升级时数据库损坏时,有时会发生这种情况。

使用以下功能更新表格:

require_once 'abstract.php';
class Fix_Shell_Index_Table_Repair extends Mage_Shell_Abstract
{

    /**
     *
     * @return Varien_Db_Adapter_Interface
     */
    protected function _getConnection()
    {
        $resource = Mage::getSingleton('core/resource');
        $writeConnection = $resource->getConnection('core_write');
        return $writeConnection;
    }

    /**
     *Delete catalog product relation table data.
     */
    protected function _processTableData()
    {

        $this->_getConnection()->query('delete from `catalog_category_product` WHERE product_id not in(select entity_id from `catalog_product_entity`);');
        echo "catalog_category_product table repaired\n";

        $this->_getConnection()->query('delete from `catalog_category_product` WHERE category_id not in(select entity_id from catalog_category_entity); ');
        echo "catalog_category_product table repaired\n";

        $this->_getConnection()->query('delete from `catalog_product_website` WHERE product_id not in(select entity_id from catalog_product_entity); ');
        echo "catalog_product_website table repaired\n";

        $this->_getConnection()->query('delete from `catalog_product_index_eav_idx` WHERE entity_id not in(select entity_id from catalog_product_entity); ');
        echo "catalog_product_index_eav_idx table repaired\n";

        $this->_getConnection()->query('delete from `catalog_product_index_eav` WHERE entity_id not in(select entity_id from catalog_product_entity); ');
        echo "catalog_product_index_eav table repaired\n";

        $this->_getConnection()->query('delete from `catalog_product_link` WHERE product_id not in(select entity_id from catalog_product_entity); ');
        echo "catalog_product_link table repaired\n";

        $this->_getConnection()->query('delete from `catalog_product_relation` WHERE parent_id not in(select entity_id from catalog_product_entity);');
        echo "catalog_product_relation table repaired\n";

        $this->_getConnection()->query('DELETE from `sales_flat_quote_shipping_rate` WHERE `address_id` not in(select `address_id` from `sales_flat_quote_address`);');
        echo "sales_flat_quote_shipping_rate table repaired\n";

        echo "All done\n";
    }

    /**
     * Run script
     *
     * @return void
     */
    public function run()
    {
        try {
            $this->_processTableData();
        } catch (Exception $e) {
            echo $e->getMessage();
        }

    }
}

$shell = new Fix_Shell_Index_Table_Repair();
$shell->run();