Wordpress - dbDelta only creates the last table
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);
}
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 :)