在为新的Wordpress插件创建表格时,我遇到了创建索引的问题。
以下是代码:
global $wpdb;
$em_posts = $wpdb->prefix . "em_collab_posts";
$em_groups = $wpdb->prefix . "em_collab_groups";
$em_users = $wpdb->prefix . "em_collab_users";
if($wpdb->get_var("SHOW TABLES LIKE '$em_users'") != $em_users) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_users` (
`cuID` bigint(20) NOT NULL,
`groupID` bigint(20) NOT NULL,
`userID` bigint(20) NOT NULL,
`cuType` int(11) NOT NULL,
`cuSettings` longtext NOT NULL,
`cuCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cuUpdated` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
// Add the indices
dbDelta("ALTER TABLE `$em_users`
ADD PRIMARY KEY (`cuID`), ADD KEY `groupID` (`groupID`), ADD KEY `userID` (`userID`), ADD KEY `cuType` (`cuType`); ");
dbDelta("ALTER TABLE `$em_users`
MODIFY `cuID` bigint(20) NOT NULL AUTO_INCREMENT;");
}
if($wpdb->get_var("SHOW TABLES LIKE '$em_posts'") != $em_posts) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_posts` (
`cpID` bigint(20) NOT NULL,
`groupID` bigint(20) NOT NULL,
`post_id` bigint(20) NOT NULL,
`cpSettings` longtext NOT NULL,
`cpCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cpUpdated` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
// Add the indices
dbDelta("ALTER TABLE `$em_posts`
ADD PRIMARY KEY (`cpID`), ADD KEY `groupID` (`groupID`), ADD KEY `post_id` (`post_id`);");
dbDelta("ALTER TABLE `$em_posts`
MODIFY `cpID` bigint(20) NOT NULL AUTO_INCREMENT;");
}
if($wpdb->get_var("SHOW TABLES LIKE '$em_groups'") != $em_groups) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_groups` (
`groupID` bigint(20) NOT NULL,
`gTitle` varchar(50) NOT NULL,
`gDescription` longtext NOT NULL,
`gSettings` longtext NOT NULL,
`gCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`gUpdated` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
// Add the indices
dbDelta("ALTER TABLE `$em_groups`
ADD PRIMARY KEY (`groupID`), ADD KEY `gTitle` (`gTitle`);");
dbDelta("ALTER TABLE `$em_groups`
MODIFY `groupID` bigint(20) NOT NULL AUTO_INCREMENT;");
}
我也尝试使用ALTER TABLE
语句进行实际创建,表格本身已创建,但索引却没有。
我做错了什么?或者有不同的方法来做到这一点吗?
答案 0 :(得分:1)
需要更改初始查询的
$em_posts = $wpdb->prefix . "em_collab_posts";
$em_groups = $wpdb->prefix . "em_collab_groups";
$em_users = $wpdb->prefix . "em_collab_users";
if($wpdb->get_var("SHOW TABLES LIKE '$em_users'") != $em_users) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_users` (
`cuID` bigint(20) NOT NULL auto_increment,
`groupID` bigint(20) NOT NULL,
`userID` bigint(20) NOT NULL,
`cuType` int(11) NOT NULL,
`cuSettings` longtext NOT NULL,
`cuCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cuUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`cuID`),
KEY `groupID` (`groupID`),
KEY `userID` (`userID`),
KEY `cuType` (`cuType`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$em_posts'") != $em_posts) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_posts` (
`cpID` bigint(20) NOT NULL auto_increment,
`groupID` bigint(20) NOT NULL,
`post_id` bigint(20) NOT NULL,
`cpSettings` longtext NOT NULL,
`cpCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cpUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`cpID`),
KEY `groupID` (`groupID`),
KEY `post_id` (`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$em_groups'") != $em_groups) {
$sql = "CREATE TABLE IF NOT EXISTS `$em_groups` (
`groupID` bigint(20) NOT NULL auto_increment,
`gTitle` varchar(50) NOT NULL,
`gDescription` longtext NOT NULL,
`gSettings` longtext NOT NULL,
`gCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`gUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`groupID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table
dbDelta($sql);
}