I’m currently writing a plugin for a customer, and while it’s usually working good, I found that dbDelta does not allow me to create the table I need on plugin activation.
I’m running the below code to bind the activation function:
register_activation_hook(__FILE__, 'adminInstallation');
And this is the function itself:
function adminInstallation(){
global $wpdb;
$objectEquipment = 'wp_object_equipment';
$equipmentSQL = "CREATE TABLE ".$objectEquipment." (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL
);";
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
$equipment = dbDelta($equipmentSQL);
}
Once this has been run, I am checking the database, but no tables have been added. Trying to dump the error will only result in WordPress telling me there was unexpected output, but it wont let me see the actual message that the server returns. This issue has been bugging me for some hours, and I cant continue until it’s solved. Does anyone here have any idea why it might do this?
As far as I can tell, all the code is valid, and this is the third plugin I’ve written. I even tried using the code from my previous ones, but that did not work either.
EDIT: I tried running the function after the plugin activation and dump the dbDelta response. It reports that the table has been created, but still, there’s nothing new in the database. Any ideas?
Thanks in advance! //
Jonathan
From http://codex.wordpress.org/Creating_Tables_with_Plugins:
You shoud
echo $wpdb->last_error;
after dbDelta call so you get the mysql error.You can try this function:
I search and dbDelta is running $wpdb->query($sqlQuery); for custom queries.
For those whose dbDelta is not working use
Thanks!
Have a nice code
I do know that dbDelta is very particular about the formatting. I believe the issue is that you are not using backticks (`) around database parts. Try the following.
On a side note, wouldn’t hurt to designate a primary key.