无法在mysql中创建外键(Wordpress)

时间:2015-09-20 12:33:47

标签: php mysql wordpress foreign-keys

我使用以下查询来创建表,但它返回有关外键的错误:

$sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        supervisor char(30) NOT NULL,
        customer char(30) NOT NULL,
        order_id int(40) NOT NULL,
        status int(5) NOT NULL,
        amount int(40) DEFAULT 0,
        FOREIGN KEY (order_id) REFERENCES wp_post(ID), 
    ) $charset_collate;";

错误:

20-Sep-2015 12:12:36] خطای You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ID') ON DELETE CASCADE
    ) DEFAULT CHARACTER SET utf8' at line 8 در پایگاه‌داده وردپرس برای دستور CREATE TABLE wp_Arvand_Marketing (
        id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        supervisor char(30) NOT NULL,
        customer char(30) NOT NULL,
        order_id int(40) NOT NULL,
        status int(5) NOT NULL,
        amount int(40) DEFAULT 0,
        FOREIGN KEY (order_id) REFERENCES post('ID') ON DELETE CASCADE
    ) DEFAULT CHARACTER SET utf8 ساخته شده توسط activate_plugin, do_action('activate_woocommerce-arvandkala/safircod.php'), call_user_func_array, adv_activate_plugins, dbDelta

1 个答案:

答案 0 :(得分:1)

你的意思是在ID列名称周围使用backtique而不是单引号;这基本上使它成为字符串文字而不是列名。

您的外键行

 REFERENCES post('ID') ON DELETE CASCADE

应该是

REFERENCES post(`ID`) ON DELETE CASCADE