Why does dbDelta() not catch MysqlErrors?

As @Charleston Software Associates mentioned, the DESCRIBE query should not be executed if the table doesn’t exist. The best solution, as he pointed out, is to prevent the error from occurring in the first place.

To do so, patch wp-admin/includes/upgrade.php as follows:

Change the following line from dbDelta():

  $tablefields = $wpdb->get_results("DESCRIBE {$table};”);

to:

    //Begin core hack - See http://wordpress.stackexchange.com/q/141971/10388
    $check_table = $wpdb->get_results("SHOW TABLES LIKE '".str_replace("_", "\_", $table)."';");
    if(!empty($check_table))
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
    else
        $tablefields = null;
    //End core hack

This isn’t the cleanest solution (especially since it’s a core hack), but it works for me, at least! Perhaps it will help someone else.

UPDATE: See my comment below. (This doesn’t fully resolve the problem, although it is a step in the right direction)

Leave a Comment