Derek Derek - 2 months ago 9
MySQL Question

Plugin not creating 2nd table on activation

I created the first table for my plugin, and after working on adding more features I realized I needed to add a second table for an additional type of data.

Troubleshooting efforts so far have resolved the issue with dbDelta() not using the same variable name for the sql data, running dbDelta() both as a combined sql var (

$setup_sql .= '...'
instead of
$setup_sql = '...'
), and also running the code as a simple function rather than a class.

I even tried running a single table creation with a different name, just to check the code worked, and it still didn't create another table.

I have had no errors running through all these tests, and for all intents and purposes it seems that Wordpress doesn't allow a plugin to create more than one table - but I know that's not the case.

class activation_setup {
public static function wpd_activate() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

$wpd_table_trig = $wpdb->prefix . 'wpd_triggers';
$setup_sql_trig = "CREATE TABLE $wpd_table_trig (
id int(11 ) NOT NULL AUTO_INCREMENT,
trigger varchar(255) DEFAULT NULL,
popup varchar(255) DEFAULT NULL,
notify varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_trig );

$wpd_table_sel = $wpdb->prefix . 'wpd_selections';
$setup_sql_sel = "CREATE TABLE $wpd_table_sel (
id int(11 ) NOT NULL AUTO_INCREMENT,
selected varchar(255) DEFAULT NULL,
session varchar(255) DEFAULT NULL,
ip varchar(255) DEFAULT NULL,
page varchar(255) DEFAULT NULL,
date varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_sel );

update_option('wpd_activated',true);
}
}
$activation_setup = new activation_setup();
register_activation_hook( __FILE__ , array( $activation_setup, 'wpd_activate' ) );


I'm going absolutely nuts trying to work out WHY this code doesn't add the second table, even after stripping out the duplicate table code and running it with a different table name.

I've through a ton of blog posts and Stack answers trying to get to the bottom of it.

Any help would be fantastic.

Answer

Issue is with the table definition. You've written trigger in your table definition it is keyword also for mysql.
That was the issue.
Your problem will be solved by this change.
Use "`" when writing field name.

 class activation_setup {
    public static function wpd_activate() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

        $wpd_table_trig = $wpdb->prefix . 'wpd_triggers';
        $setup_sql_trig = "CREATE TABLE $wpd_table_trig (
          `id` int(11 ) NOT NULL AUTO_INCREMENT,
          `trigger` varchar(255) DEFAULT NULL,
          `popup` varchar(255) DEFAULT NULL,
          `notify` varchar(255) DEFAULT NULL,
          UNIQUE KEY id (id)
        ) $charset_collate;";
        dbDelta( $setup_sql_trig );

        $wpd_table_sel = $wpdb->prefix . 'wpd_selections';
        $setup_sql_sel = "CREATE TABLE $wpd_table_sel (
          `id` int(11 ) NOT NULL AUTO_INCREMENT,
          `selected` varchar(255) DEFAULT NULL,
          `session` varchar(255) DEFAULT NULL,
          `ip` varchar(255) DEFAULT NULL,
          `page` varchar(255) DEFAULT NULL,
          `date` varchar(255) DEFAULT NULL,
          UNIQUE KEY id (id)
        ) $charset_collate;";
        dbDelta( $setup_sql_sel );


    }
}
$activation_setup = new activation_setup();
register_activation_hook( __FILE__ , array( $activation_setup, 'wpd_activate' ) );  

If you want to make sure that issue is with that trigger then remove "`" from trigger field and then again activate plugin.