The queries below (assigned to php variables) seem to have an issue on the 2nd query, where I set the foreign key to the "couponId" column on the first table. My IDE lints "couponId" (inside the foreign key declaration) as well to show there's a issue.
Does anyone see what the issue is? Below is the error and the create function/query
// codeception is a test runner so I think it injects "temporary" into create statements
WordPress database error Cannot add foreign key constraint for query CREATE TEMPORARY TABLE IF NOT EXISTS wp_frequentVisitorCoupons_targets (
targetId mediumint not null auto_increment unique,
primary key (targetId),
isSitewide tinyint(1) not null,
targetUrl varchar(500),
displayThreshold tinyint(5) not null default 20,
offerCutoff tinyint(5),
fk_coupons_targets mediumint not null unique,
foreign key (fk_coupons_targets) references wp_frequentVisitorCoupons_coupons(couponId) on delete cascade
) made by Codeception\Application->run, <continues on ...>
// the create table function
public static function createTablesIfNotExists() {
global $wpdb;
$createCouponTableQuery = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}fvc_coupons (
couponId mediumint not null auto_increment unique,
primary key (couponId),
totalHits mediumint not null,
isText boolean not null,
fileName varchar(200),
folderDateString varchar(7)
)";
$createTargetTableQuery = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}fvc_targets (
targetId mediumint not null auto_increment unique,
primary key (targetId),
isSitewide tinyint(1) not null,
targetUrl varchar(500),
displayThreshold tinyint(5) not null default 20,
offerCutoff tinyint(5),
fk_coupons_targets mediumint not null unique,
foreign key (fk_coupons_targets) references {$wpdb->prefix}fvc_coupons(couponId) on delete cascade
)";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($createCouponTableQuery);
dbDelta($createTargetTableQuery);
}