WordPress dbDelta not functioning

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:

Read More
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

Related posts

Leave a Reply

5 comments

  1. You can try this function:

    $table_name = "ratings";
    
    $table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT,
                        rate tinyint(1) NOT NULL,
                        ticket_id bigint(20) NOT NULL,
                        response_id bigint(20) NOT NULL,
                        created_at TIMESTAMP";
    
    $table_keys = "PRIMARY KEY (id),
                        KEY ratings_rate (rate),
                        UNIQUE KEY ratings_response_id (response_id)";
    
    create_table($table_name, $table_columns, $table_keys);
    
  2. I search and dbDelta is running $wpdb->query($sqlQuery); for custom queries.

    For those whose dbDelta is not working use

    dbDelta($sql_query_to_create_table);
    

    Thanks!
    Have a nice code

  3. 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.

    $equipmentSQL = "CREATE TABLE `".$objectEquipment."` (
      `id` mediumint(9) NOT NULL AUTO_INCREMENT,
      `name` tinytext NOT NULL,
      PRIMARY KEY (`id`)
    );";