plugin development - Creating two tables in database on activation hook

admin2025-06-02  4

I have been trying to generate two tables in database on activation hook. But it generates only the first one table on activation hook . What am i doing wrong in my code?

class Datetimepicker_Tables{

function __construct(){
    add_action('init',array($this,'create_tables'));

}

function activate(){
    $this->create_tables();
    flush_rewrite_rules();
}

function create_tables(){
        global $wpdb;

        $date_table_name = $wpdb->prefix . 'booking_dates';
        $time_table_name = $wpdb->prefix . 'booking_timeslots';
        $charset_collate = $wpdb->get_charset_collate();

        $date_table = "CREATE TABLE $date_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
             `year` int(11) NOT NULL,
             `month` int(11) NOT NULL,
             `day` int(11) NOT NULL,
             PRIMARY KEY (`id`)
            ) $charset_collate;";

        $time_table = "CREATE TABLE $time_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `bid` int(11) NOT NULL,
            `time` int(11) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `bid` (`bid`),
            CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)
            )  $charset_collate;";  

            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $date_table );
            dbDelta( $time_table );
}
}

$tablesclass = new Datetimepicker_Tables();
register_activation_hook(__FILE__,array($tablesclass,'activate'));

I have been trying to generate two tables in database on activation hook. But it generates only the first one table on activation hook . What am i doing wrong in my code?

class Datetimepicker_Tables{

function __construct(){
    add_action('init',array($this,'create_tables'));

}

function activate(){
    $this->create_tables();
    flush_rewrite_rules();
}

function create_tables(){
        global $wpdb;

        $date_table_name = $wpdb->prefix . 'booking_dates';
        $time_table_name = $wpdb->prefix . 'booking_timeslots';
        $charset_collate = $wpdb->get_charset_collate();

        $date_table = "CREATE TABLE $date_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
             `year` int(11) NOT NULL,
             `month` int(11) NOT NULL,
             `day` int(11) NOT NULL,
             PRIMARY KEY (`id`)
            ) $charset_collate;";

        $time_table = "CREATE TABLE $time_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `bid` int(11) NOT NULL,
            `time` int(11) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `bid` (`bid`),
            CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)
            )  $charset_collate;";  

            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $date_table );
            dbDelta( $time_table );
}
}

$tablesclass = new Datetimepicker_Tables();
register_activation_hook(__FILE__,array($tablesclass,'activate'));
Share Improve this question asked Mar 6, 2019 at 11:39 Afzal KhanAfzal Khan 54 bronze badges 5
  • REFERENCES booking_dates should be REFERENCES $date_table_name. – Sally CJ Commented Mar 6, 2019 at 12:23
  • Are you talking about this -> "$date_table_name = $wpdb->prefix . 'booking_dates';" ? – Afzal Khan Commented Mar 6, 2019 at 12:27
  • Yes, the table name. – Sally CJ Commented Mar 6, 2019 at 12:33
  • I don't think so, i made a variable and passing a name of the table in that variable. – Afzal Khan Commented Mar 6, 2019 at 12:39
  • I was saying that you didn't use the proper table name in the CONSTRAINT clause.. – Sally CJ Commented Mar 6, 2019 at 13:00
Add a comment  | 

1 Answer 1

Reset to default 1

You're not using the proper table name in your CONSTRAINT clause:

CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)

That should be:

CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `$date_table_name` (`id`)

where $date_table_name (as I could see) is the correct table name.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748837249a314135.html

最新回复(0)