问题
如果表已经存在,您的代码仍将尝试执行以下查询:
1) ALTER TABLE wp_voicemail_call CHANGE COLUMN user_id user_id BIGINT(9) UNSIGNED NOT NULL
2) ALTER TABLE wp_voicemail_call CHANGE COLUMN call_id call_id BIGINT(9) UNSIGNED NOT NULL
3) ALTER TABLE wp_voicemail_call CHANGE COLUMN opened opened BOOL DEFAULT 0 NOT NULL
4) ALTER TABLE wp_voicemail_call ADD
5) ALTER TABLE wp_voicemail_call ADD PRIMARY KEY (user_id, call_id)
请注意,此查询:
ALTER TABLE wp_voicemail_call ADD PRIMARY KEY (user_id, call_id)
正在尝试添加另一个已定义的主键,我们只能使用其中一个。因此出现了错误。
此查询:
ALTER TABLE wp_voicemail_call ADD
来自
empty line 高于
PRIMARY KEY
线
在dbDelta()
功能有this part 这将取消主要部分的设置:
foreach ( $index_strings as $index_string ) {
if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
unset( $indices[ $aindex ] );
break
}
}
但由于某些原因,在您的情况下,数组搜索总是返回false。
我挖得更深,在你的情况下$indices
阵列为:
Array
(
[0] =>
[1] => PRIMARY KEY (user_id, call_id)
)
但是
$index_strings
数组为
Array
(
[0] => PRIMARY KEY (user_id,call_id)
[1] => PRIMARY KEY (user_id,call_id)
)
因此,我们可以看到不匹配:
PRIMARY KEY (user_id,call_id)
对
PRIMARY KEY (user_id, call_id)
。。。单一空间!!
建议的解决方案是,如果我们删除多余的空行和多余的空间:
$sql = "CREATE TABLE {$wpdb->prefix}voicemail_call (
user_id BIGINT(9) UNSIGNED NOT NULL,
call_id BIGINT(9) UNSIGNED NOT NULL,
opened BOOL DEFAULT 0 NOT NULL,
PRIMARY KEY (user_id,call_id)
);";
那么我们应该只运行这些查询
dbDelta()
:
1) ALTER TABLE wp_voicemail_call CHANGE COLUMN user_id user_id BIGINT(9) UNSIGNED NOT NULL
2) ALTER TABLE wp_voicemail_call CHANGE COLUMN call_id call_id BIGINT(9) UNSIGNED NOT NULL
3) ALTER TABLE wp_voicemail_call CHANGE COLUMN opened opened BOOL DEFAULT 0 NOT NULL
当表已存在时。