外键的DBDelta支持

时间:2012-05-21 作者:Gaia

On PHP 5.3.13/MySQL 5.5.21 the following code doesn\'t work:

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);
 }

The code provider suggested a downgrade to MySQL 5.1.37 (no, thanks) or the following update:

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);
   }
这似乎是解决问题的一种相当肮脏的方式(没有级联删除/更新)。因此:

  1. Do I really have to live with that until dbDelta supports FOREIGN KEY?
  2. Is it true that dbDelta only works with foreign key in a 3 year old MySQL version?

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

在dbDelta支持外键之前,我真的必须接受这种情况吗?

坦白说,是的。但这就是开源的美妙之处——欢迎任何人发布补丁!

However, 将其扩展到模式设计的其他方面几乎肯定会带来不必要的复杂性;提高失败的可能性——这是核心团队事先会强烈考虑的。

我会接受@xav0989的建议-使用dbDelta 根据其意图(基本表实现、列添加和调整),并使用$wpdb.

结束

相关推荐

直接在MySQL DB中更改Author slug(USER_NICENAME字段)是个好主意吗?

在搜索如何更改author slug时,我找到了两种方法:更改user_nicename 在WordPress站点的MySQL数据库中,将字段设置为您想要的slug。Changing the Author Slug from Username to Nickname</(2)是毫无疑问的,因为我希望slug是自定义的,而不是昵称/昵称。至于(1),我想知道这是否可以--更改user_nicename 用户数据库中的字段?