外键的DBDelta有问题吗?

时间:2011-09-08 作者:redconservatory

我对dbDelta使用以下sql:

$sql .= "CREATE TABLE " . $location_table . " (
            location_id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
            name VARCHAR (100),
            street_no VARCHAR (5),
            street_name VARCHAR (75),
            city VARCHAR (75),
            province_state VARCHAR (75),
            postal_code VARCHAR(10),
            country VARCHAR (75),
            post_page_url VARCHAR(300),
            icon_id MEDIUMINT(9),
            PRIMARY KEY  (location_id),
            FOREIGN KEY (icon_id) REFERENCES ".$wpdb->prefix."nc_icon (icon_id)
            );";
我收到错误:

WordPress database error: [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 \'FOREIGN KEY (icon_id) REFERENCES wp_nc_icon (icon_id)\' at line 1]
ALTER TABLE wp_nc_location ADD COLUMN FOREIGN KEY (icon_id) REFERENCES wp_nc_icon (icon_id)
似乎不是写:

ALTER TABLE wp_nc_location ADD CONSTRAINT FOREIGN KEY (icon_id) REFERENCES wp_nc_icon (icon_id)
DBDelta正在尝试写入:

ALTER TABLE wp_nc_location ADD COLUMN FOREIGN KEY (icon_id) REFERENCES wp_nc_icon (icon_id)
然而,看起来好像正在创建列icon\\u id,这正是我所需要的。

实际上这很奇怪,因为icon\\u id也是一个外键(当我查看表结构时)。

我只想消除我的“意外输出”错误。有没有办法让DBDelta接受外键?

1 个回复
SO网友:Gaia

一般来说,dbDelta does not yet support FOREIGN KEY, 尽管有人告诉我它在MySQL 5.1上可用(我可以确认它在MySQL 5.5上不可用)。

结束