8

I'm using following install function with dbDelta as referring to the codex, articles on SO I watched out for the typical problems with e.g. one space instead of two spaced at the PRIMARY KEY ...

function myplugin_install(){
    global $wpdb;
    $table_name = $wpdb->prefix . "vehicles";       

    $sql = "CREATE TABLE IF NOT EXISTS $table_name 
    (
        id                  INT UNSIGNED NOT NULL AUTO_INCREMENT,
        description         VARCHAR(25) NOT NULL,
        PRIMARY KEY  (id)
    );  
    ";

    $sql .= "CREATE TABLE IF NOT EXISTS $table_name1 
    (
        ...
    )";

    $sql .= "CREATE TABLE IF NOT EXISTS $table_nameX 
    (
        id                  INT UNSIGNED NOT NULL AUTO_INCREMENT ,
        art_alarmierung     VARCHAR(25) NOT NULL ,
        PRIMARY KEY  (id)
    );";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
echo $wpdb->last_error;

}

dbDelata only stores the last $sql variable. And echo $wpdb->last_error; don't show any errors? Where is my failure?

BR; mybecks

9

Run dbDelta for each SQL statement seperately:

function myplugin_install(){
    global $wpdb;
    $table_name = $wpdb->prefix . "vehicles";       
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $sql = "CREATE TABLE IF NOT EXISTS $table_name 
    (
        id                  INT UNSIGNED NOT NULL AUTO_INCREMENT,
        description         VARCHAR(25) NOT NULL,
        PRIMARY KEY  (id)
    );  
    ";
    dbDelta($sql);

    $sql2 = "CREATE TABLE IF NOT EXISTS $table_name1 
    (
        ...
    )";
    dbDelta($sql2);

    $sql3 = "CREATE TABLE IF NOT EXISTS $table_nameX 
    (
        id                  INT UNSIGNED NOT NULL AUTO_INCREMENT ,
        art_alarmierung     VARCHAR(25) NOT NULL ,
        PRIMARY KEY  (id)
    );";

    dbDelta($sql3);

}
1
  • works like charm. I tried all .. creating an array with sql expression, but won't work too. And don't get this easy solution in my mind :)
    – mybecks
    May 9 '12 at 17:30
8

For those trying to understand how some plugins, like WooCommerce, are able to do that without calling dbDelta for every table, the reason is the IF NOT EXISTS part of query. The dbDelta function creates an index based on what comes after CREATE TABLE, so it ends up guessing that all queries create the same table.

If you got here, try to use just CREATE TABLE tablename and it will work :)

2
  • This is a good explanation. I can't tell from the Codex, but does DB delta try to create a table even if one exists and then throws an error?
    – wlh
    Oct 18 '18 at 15:04
  • It tries to figure out what would be the necessary SQL query to alter that table and make it like the current CREATE TABLE statement. Give a look at lines ~2414 here: developer.wordpress.org/reference/functions/dbdelta Oct 19 '18 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.