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
|
1 Answer
Reset to default 1You'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.
booking_dates
should beREFERENCES $date_table_name
. – Sally CJ Commented Mar 6, 2019 at 12:23CONSTRAINT
clause.. – Sally CJ Commented Mar 6, 2019 at 13:00