問題描述
在 PHP 5.3.13 /MySQL 5.5.21 中,以下程式碼不起作用:
if($check_custom_fields_form!=1){
$sql = "CREATE TABLE IF NOT EXISTS ". $table_custom_fields_form ." (
`form_name` longtext NOT NULL,
`field_id` bigint(20) NOT NULL,
FOREIGN KEY (`field_id`) REFERENCES $table_custom_fields (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci";
dbDelta($sql);
}
if($check_subscribe_cat!=1){
$sql = "CREATE TABLE IF NOT EXISTS ". $table_subscribe_cat ." (
`subscribe_id` bigint(20) NOT NULL,
`cat_id` bigint(20) NOT NULL,
FOREIGN KEY (`subscribe_id`) REFERENCES ".$wpdb->prefix."tgt_subscription (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`cat_id`) REFERENCES ".$wpdb->prefix."terms (`term_id`) ON DELETE CASCADE ON UPDATE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci";
dbDelta($sql);
}
code provider 建議降級為 MySQL 5.1.37(不,謝謝) 或以下更新:
if($check_custom_fields_form!=1){
$sql = "CREATE TABLE IF NOT EXISTS ". $table_custom_fields_form ." (
`form_name` longtext NOT NULL,
`field_id` bigint(20) NOT NULL,
KEY(field_id)
) CHARACTER SET utf8 COLLATE utf8_general_ci";
dbDelta($sql);
}
if($check_subscribe_cat!=1){
$sql = "CREATE TABLE IF NOT EXISTS ". $table_subscribe_cat ." (
`subscribe_id` bigint(20) NOT NULL,
`cat_id` bigint(20) NOT NULL,
KEY(subscribe_id),
KEY(cat_id)
) CHARACTER SET utf8 COLLATE utf8_general_ci";
dbDelta($sql);
}
這似乎是一個相當髒的方式來解決問題 (沒有級聯刪除/更新) 。因此:
-
我真的要和那個 until dbDelta supports FOREIGN KEY 一起生活嗎?
-
dbDelta 是否只能在 3 年的 MySQL 版本中使用外部索引鍵?
最佳解決方案
Do I really have to live with that until dbDelta supports FOREIGN KEY?
坦白說,是的。但這是開源的美麗 – 任何人都歡迎發貼!
然而,擴充套件它以涵蓋模式設計的其他方面幾乎肯定會引起不必要的複雜性提高失敗的可能性 – 核心團隊將事先強烈考慮。
我會採取 @ xav0989 的建議 – 使用 dbDelta 的意圖 (基本表實現,列新增和調整),並使用 $wpdb 處理附加功能。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。