Your SQL command is invalid. The error says:
WordPress database error: [Incorrect table definition; there can be only one auto column and it must be defined as a key]
CREATE TABLE to_issues ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(125) NOT NULL )
Try:
$sql ="CREATE TABLE {$wpdb->prefix}to_issues (
id int(11) unsigned NOT NULL auto_increment,
name varchar(125) NOT NULL,
PRIMARY KEY (id)
);";
Note that dbDelta()
is very particular about what is passed to it. For instance you need two spaces between the PRIMARY KEY
and (id)
.
I have added $wpdb->prefix
to the table – this adds the (user specified) WordPress prefix. It’s good practise to use this, as some people have more than one WordPress install in their database.
dbDelta
The ‘register activation’ is hook only fired when the plug-in is activated – but not when the plug-in is upgraded. So when a user deactivates and activates the plug-in, dbDelta($sql)
is fired – this is intended. dbDelta
(see source) recognises ‘CREATE’ queries, and first checks if the table already exists – and if it doesn’t, it creates the table. If the table does exists, it checks that each of the fields match that in the given $sql
, and if not, updates them. If the field doesn’t exist, but is specified in your $sql
, it is created.
This way you can easily update a table by chaging $sql
. However, on updating a plug-in, to_install
won’t called – so you need to manually call it on admin_init
after checking that the current ‘database version’ (TO_VERSION
) is an old one. See this post for a detailed explanation of activation/deactivation/uninstall/update procedures..