Wordpress使用FOREIGN KEY创建数据库

时间:2016-01-10 16:35:07

标签: mysql wordpress

我正在尝试创建一个wordpress插件,它将使用两个数据库,我需要它们才能拥有FOREIGN KEY,但只有在我删除FOREIGN KEY时才能创建它们,我可以让它们创建一个表。

global $wpdb;
  $table_name = $wpdb->prefix . 'aa_accessform';
  $table_name2 = $wpdb->prefix . 'aa_formcomments';

  $charset_collate = $wpdb->get_charset_collate();

    $wpdb->query(
        "CREATE TABLE IF NOT EXISTS $table_name (
            id              INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            customer_id     INT UNSIGNED NOT NULL,
            appointment_id  INT UNSIGNED NOT NULL,
            notes TEXT,
            token VARCHAR(255) DEFAULT NULL
        ) ENGINE = INNODB
        DEFAULT CHARACTER SET = utf8
        COLLATE = utf8_general_ci"
    );
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
    $wpdb->query(
        "CREATE TABLE IF NOT EXISTS $table_name2 (
            id              INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            customer_codes__id     INT UNSIGNED NOT NULL,
            appointment_codes_id  INT UNSIGNED NOT NULL,
            notes TEXT,
            token VARCHAR(255) DEFAULT NULL,
            PRIMARY KEY  (id),
            FOREIGN KEY  (customer_codes__id) REFERENCES ".$table_name." (id)
        ) ENGINE = INNODB
        DEFAULT CHARACTER SET = utf8
        COLLATE = utf8_general_ci"
    );
    dbDelta( $sql );

我很想知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

我找到了答案,现在正在开展以下工作。

*

请注意以下行是真正的修复 FOREIGN KEY(customer_codes_id)REFERENCES $ table_name(id)//正确 下面的错误行是: FOREIGN KEY(customer_codes__id)REFERENCES"。$ table_name。" (id)//错误