修復 Discuz! 從 7.2 轉換到 X 時的資料庫錯誤

轉換工具提示重複主鍵

使用轉換工具進行轉換時,會遇到莫名的主鍵重複錯誤,提示 #1062 - Duplicate entry '...' for key PRIMARY。一番找尋,相關的結果都說「刪掉了就好了」,問題是刪了一個又出一個,最後逼得淺羽直接用 SQL 批量刪了五六千條,下一個還是有問題。

然而原來的資料庫也是 MariaDB,也指定了 PRIMARY KEY,怎麼會重複呢?

囧,这个是转换程序的一个Bug,
我开始的想法和你一样,删除重复的记录,
结果没用,其它记录又会出错,
后来才知道是因为转换程序里的SQL语句没有order by,
所以…

CzBiX 2011-04-24
Discuz7.2升级至DX1.5数据表转换时出错的解决方法

知道了原理,修復起來就比較簡單了:

--- Discuz_X1.5_SC_UTF8/utility/convert/source/d7.2_x1.5/table/posts.php    2011-12-20 16:58:50.000000000 +0800
+++ bbs/convert/source/d7.2_x1.5/table/posts.php                            (new)
@@ -20,7 +20,7 @@
        $db_target->query("TRUNCATE $table_target");
 }

-$query = $db_source->query("SELECT * FROM $table_source WHERE pid>'$start' LIMIT $limit");
+$query = $db_source->query("SELECT * FROM $table_source WHERE pid>'$start' ORDER BY pid ASC LIMIT $limit");
 while($row = $db_source->fetch_array($query)) {
        $nextid = $row['pid'];
        $row = daddslashes($row, 1);

保存,再執行轉換程序,這次就很順利了。

Comments

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post’s permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Find out more about Webmentions.)