2012-03-13 116 views
3

我收到以下錯誤,當我作出命令,信用卡/借記卡在Magento:Magento的訂單節約錯誤:完整性約束違規

Order saving error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '282-189' for key 2 

我不能得到一個確切的蹤跡,因爲它只是沒我不想向我證明這一點,但是,我已將其追溯到501(未編輯)的app/code/core/Mage/Checkout/controllers/OnepageController.php。確切的行是:

$this->getOnepage()->saveOrder(); 

現在,邏輯告訴我這是具有僅僅兩個命令嘗試添加一個訂單時相同的密鑰,還是我完全錯了嗎?

無論如何,在這之外,最好的方法是什麼來解決這個問題?我曾想過導出訂單,全部刪除它們,然後重新導入它們,但是我感覺它不起作用。

我使用的Magento 1.6.2.0

編輯:我剛剛意識到,如果我發現哪個表失敗的完整性檢查,我可能是空表(取決於哪個表是),這將修理它自己?任何想法如何找出哪個桌子搞亂了?

EDIT2:所以奧古茲Çelikdemir的回答後,原來下面是罪魁禍首:

2012-03-14T13:59:01+00:00 DEBUG (7): SQL: INSERT INTO `sales_order_tax_item` (`tax_id`, `item_id`, `tax_percent`) VALUES (?, ?, ?) (254, 8, 10) 
2012-03-14T13:59:01+00:00 DEBUG (7): SQL: INSERT INTO `sales_order_tax_item` (`tax_id`, `item_id`, `tax_percent`) VALUES (?, ?, ?) (254, 8, 10) 

顯然兩個相同tax_id不能插入。任何人有任何想法如何解決它?

+0

最近,你有沒有安裝任何擴展? – 2012-03-14 14:33:24

+0

是的,但沒有什麼應該干擾稅!儘管我找到了解決方案。 – 2012-03-14 15:28:26

回答

2

我發現了一個快速骯髒的黑客。我會嘗試將這作爲bug發佈給開發人員。 打開文件app/code/core/Mmage/Tax/Model/Observer.php並進入管線132 有一個if($item)聲明,該聲明中,環繞此代碼:

$define = 'FIX_'.$item->getId().'_'.$result->getTaxId(); 
if(!defined($define) || constant($define) != true) { 
      // code 
    define($define, true); 
} 

例子:

if (isset($ratesIdQuoteItemId[$id])) { 
    foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) { 
     if ($quoteItemId['code'] == $tax['code']) { 
      $item = $order->getItemByQuoteItemId($quoteItemId['id']); 
      if ($item) { 
       $define = 'FIX_'.$item->getId().'_'.$result->getTaxId(); 
       if(!defined($define) || constant($define) != true) { 
        $data = array(
         'item_id'  => $item->getId(), 
         'tax_id'  => $result->getTaxId(), 
         'tax_percent' => $quoteItemId['percent'] 
        ); 
        Mage::getModel('tax/sales_order_tax_item')->setData($data)->save(); 
        define($define, true); 
       } 
      } 
     } 
    } 
} 
3

截斷以下log表。爲了保護您自己,請從CONSOLE進行備份,而不是在Magento中。

TRUNCATE `log_customer`; 
ALTER TABLE `log_customer` AUTO_INCREMENT=1; 
TRUNCATE `log_quote`; 
ALTER TABLE `log_quote` AUTO_INCREMENT=1; 
TRUNCATE `log_summary`; 
ALTER TABLE `log_summary` AUTO_INCREMENT=1; 
TRUNCATE `log_visitor_info`; 
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1; 
TRUNCATE `log_url`; 
ALTER TABLE `log_url` AUTO_INCREMENT=1; 
TRUNCATE `log_url_info`; 
ALTER TABLE `log_url_info` AUTO_INCREMENT=1; 
TRUNCATE `log_visitor`; 
ALTER TABLE `log_visitor` AUTO_INCREMENT=1; 
TRUNCATE `report_event`; 
ALTER TABLE `report_event` AUTO_INCREMENT=1; 
TRUNCATE `log_summary_type`; 
ALTER TABLE `log_summary_type` AUTO_INCREMENT=1; 

編輯: 對於SQL調試,打開/lib/Varien/Db/Adapter/Pdo/Mysql.php文件,並找到protected $_debug。默認狀態應爲false,更改爲true

之後,日誌文件應在var/debug/sql.txt

也可,藉此計算器ansver How do I print all the queries in Magento?

編輯2: 所以,這裏是你的文件處理銷售項目的稅收。 app/code/core/Mage/Tax/Model/Observer/Observer.php

# Line 144 (Magento 1.6 edition) 
Mage::getModel('tax/sales_order_tax_item')->setData($data)->save(); 

這是調用sales_order_tax_item模型。

app/core/Mage/Tax/Model/Resource/Sales/Order/Tax/Itemp.php

# Line 51 
public function getTaxItemsByItemId($item_id) 
{ 
    $adapter = $this->_getReadAdapter(); 
    $select = $adapter->select() 
     ->from(array('item' => $this->getTable('tax/sales_order_tax_item')), array('tax_id', 'tax_percent')) 
     ->join(
      array('tax' => $this->getTable('tax/sales_order_tax')), 
      'item.tax_id = tax.tax_id', 
      array('title', 'percent', 'base_amount') 
     ) 
     ->where('item_id = ?', $item_id); 

    return $adapter->fetchAll($select); 
} 

這裏是從觀察調用該函數。那麼,你現在可以做什麼,只需在Observer或Function上添加一個斷點並查看調試器輸出。爲什麼兩次調用這個函數!

+0

沒有,沒有工作!我以前也嘗試過,但仍然無法使用。有人列出了許多其他表,包括sales_order_flat表。但沒有一個成功。還有其他建議嗎? – 2012-03-14 10:22:33

+1

您是否嘗試刷新緩存並重新索引產品和類別!從控制檯中刪除'cache'目錄而不是magento。 – 2012-03-14 10:42:42

+0

是的,我做了,我已經更新了這個問題,也許你可以幫我編輯一下這個位? – 2012-03-14 10:51:49

2

請到的部分Sales -> Tax -> Manage Tax Rules管理員面板並檢查所有記錄中是否存在重複關係區域級別。

在我來說,我有其中有稅收類「應稅貨物」,並在稅收規則我有這個類的兩個條目每個條目是用相同的稅區的產品在結帳頁面相同的錯誤。因此,該系統試圖對產品應用相同的稅款兩次。

+0

謝謝,正是我的問題。由於Magento以前的安裝(我們的版本是1.6.2.0)的稅收損失如何,由於對稅收系統進行了大量的更改,因此這只是1.9的頭部。 – evensis 2015-01-30 17:22:09

相關問題