重建索引Magento时出现约束违规/重复键

use*_*021 4 pdo constraints duplicates magento zend-db

我正在使用Magento CE 1.6.2,我的reindexer有问题(url_rewrite)

php shell/indexer.php --reindex catalog_url
Catalog URL Rewrites index process unknown error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '33432700_1343855802-0-1' for key 'UNQ_CORE_URL_REWRITE_ID_PATH_IS_SYSTEM_STORE_ID'' in /home/website/public_html/lib/Zend/Db/Statement/Pdo.php:228
Run Code Online (Sandbox Code Playgroud)

当我截断core_url_rewrite ...并且第一次通过后端点击索引器时,一切都很好,我的url重写存储在core_url_rewrites中...但是如果我第二次启动索引器(没有刷新表) ),我得到重复键的错误.

这是我的桌子的屏幕截图:https://www.dropbox.com/s/6v9uawp5v437w3h/seo_Magewroks.png

注意:UNQ_CORE_URL_REWRITE_ID_PATH_IS_SYSTEM_STORE_ID是一个索引键

我怎样才能找到问题的根源?

小智 5

这应该解决问题,

复制核心文件:/app/code/core/Mage/Catalog/Model/Resource/Url.php到:/app/code/local/Mage/Catalog/Model/Resource/Url.php

找到这个功能:

public function saveRewriteHistory($rewriteData)
{
    $rewriteData = new Varien_Object($rewriteData);
    // check if rewrite exists with save request_path
    $rewrite = $this->getRewriteByRequestPath($rewriteData->getRequestPath(), $rewriteData->getStoreId());

    if ($rewrite === false) {
        // create permanent redirect
        $this->_getWriteAdapter()->insert($this->getMainTable(), $rewriteData->getData());
    }

    return $this;

}
Run Code Online (Sandbox Code Playgroud)

替换为:

protected $_processedRewrites = array();   // add this to your class vars on top

public function saveRewriteHistory($rewriteData)
{
    $rewriteData = new Varien_Object($rewriteData);
    // check if rewrite exists with save request_path
    $rewrite = $this->getRewriteByRequestPath($rewriteData->getRequestPath(), $rewriteData->getStoreId());
    $data = $rewriteData->getData();

    $current = $data["id_path"]."_".$data["is_system"]."_".$data["store_id"];
    if ($rewrite === false && !in_array($current, $this->_processedRewrites)) {
        $this->_processedRewrites[] = $current;
        // create permanent redirect
        $this->_getWriteAdapter()->insert($this->getMainTable(), $rewriteData->getData());
    }

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

问题是因为函数检查是数据库,以便在插入之前查看core_url_rewrites中是否存在重写.这很好.但它使用以下属性进行检查:request_path,is_system,store_id

我们的问题是有些行有重复的id_path但是有不同的request_path ...这很奇怪,不知道为什么它不应该...

但是使用这个替换函数,它还将检查之前是否处理过id_path,如果是,则不会插入它.它解决了这个问题..

但是,我们仍然不知道问题的根源