2014-01-10 82 views
2

由於某種原因,我的Django Web應用程序中的MySQL表格使用不同的格式,並且導致外鍵約束錯誤(如this question)。將表InnoDB轉換爲MyISAM時的「外鍵約束失敗」

以下是表格格式的樣子。

SHOW TABLE STATUS WHERE `Name` in ('') ... 
+----------------------------+--------+---------+------------+--------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+--------------------- 
| Name      | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time   | Update_time   | Check_time   
+----------------------------+--------+---------+------------+--------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+--------------------- 
| table_0     | MyISAM |  10 | Dynamic | 341 |    83 |  28472 | 281474976710655 |  19456 |   0 |   346 | 2013-09-05 15:52:11 | 2014-01-10 14:17:18 | 2013-09-05 15:52:11 
| table_1     | MyISAM |  10 | Dynamic | 66422 |    49 |  3297260 | 281474976710655 |  2025472 |   0 |   141146 | 2013-07-20 19:12:24 | 2014-01-10 19:20:21 | 2013-07-20 19:12:27 
| table_2     | MyISAM |  10 | Dynamic |  3 |    53 |   160 | 281474976710655 |   3072 |   0 |    4 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 
| table_3     | MyISAM |  10 | Dynamic |  8 |    99 |   796 | 281474976710655 |   3072 |   0 |    10 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 
| table_4     | MyISAM |  10 | Dynamic |  0 |    0 |   0 | 281474976710655 |   1024 |   0 |    1 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 | 2013-07-20 19:12:27 
| table_5     | InnoDB |  10 | Compact |  0 |    0 |  16384 |    0 |  16384 | 7340032 |    1 | 2013-11-20 14:04:02 | NULL    | NULL     
| table_6     | InnoDB |  10 | Compact |  0 |    0 |  16384 |    0 |  16384 | 7340032 |    1 | 2013-11-20 14:03:39 | NULL    | NULL     
| table_7     | MyISAM |  10 | Dynamic | 860 |   125 |  107624 | 281474976710655 |  25600 |   0 |   977 | 2013-11-20 14:04:35 | 2014-01-06 18:32:57 | 2013-11-20 14:04:35 
| table_8     | MyISAM |  10 | Fixed  | 6632 |    21 |  139272 | 5910974510923775 |  173056 |   0 |   6753 | 2013-07-20 19:16:47 | 2014-01-09 11:26:36 | 2013-07-20 19:16:47 

我試圖更新表格格式,如this question中所述。但收到以下錯誤。

mysql> SET FOREIGN_KEY_CHECKS=0; 
mysql> alter table ztrap_emailreportoptin engine = MyISAM; 
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails 

我該怎麼做讓表間的引擎一致?

(此外,任何想法如何發生這種情況,首先?)

回答

4

MyISAM does not support foreign keys。 要轉換爲MyISAM,您需要首先刪除所有外鍵約束。

您的錯誤消息告訴您,您的表ztrap_emailreportoptin要麼引用另一個表,要麼被另一個表引用。

可以列出這樣的引用與:

SELECT 
    table_name, column_name, constraint_name, 
    referenced_table_name, referenced_column_name 
FROM information_schema.key_column_usage 
WHERE referenced_table_name IS NOT NULL; 

不過,我會建議你轉換表InnoDB的替代。

+0

這工作和解釋是有道理的。謝謝。 – bbrame

-1

嘗試轉換的MyISAM那些InnoDB的,如

alter table ztrap_emailreportoptin engine = InnoDB; 

這種方式,您將有服務器強制執行外鍵檢查所有表格。

+0

安迪,該表已經使用InnoDB。其餘的表使用MyISAM,我想讓它們都成爲MyISAM。 – bbrame