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 ( … )”; … Read more

dbDelta support for FOREIGN KEY

Do I really have to live with that until dbDelta supports FOREIGN KEY? Quite frankly, yes. But that’s the beauty of open source – anyone is welcome to post a patch! However, expanding it to cover other aspects of schema design would almost certainly incur unwanted complexity & heighten the possibility of failure – something … Read more

Does dbDelta delete columns as well?

As far as I know, dbDelta() is primarily used to add tables to the database. It can also add or alter columns. To delete columns, you should use $wpdb->query(): global $wpdb; $table = $wpdb->prefix . ‘table_name’; $wpdb->query( “ALTER TABLE $table DROP COLUMN column_name” );

dbDelta not creating tables

From WordPress-codex about dbDelta: The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary, so it can be very handy for updates (see wp-admin/upgrade-schema.php for more examples of how to use dbDelta). Note that the dbDelta function is rather picky, however. … Read more