sql - MySql - Sequalize - Cannot add foreign key constraint -
i trying using nodejs sequelize create database. commands being invoked are
create table if not exists `wheel` (`id` integer not null auto_increment , `createdat` datetime not null, `updatedat` datetime not null, `shopid` varchar(255), primary key (`id`), foreign key (`shopid`) references `shop` (`id`) on delete set null on update cascade) engine=innodb; create table if not exists `segments` (`segmentid` varchar(255) not null , `heading` varchar(255) not null, `subheading` varchar(255) not null, `createdat` datetime not null, `updatedat` datetime not null, `wheelid` integer, primary key (`segmentid`), foreign key (`wheelid`) references `wheel` (`id`) on delete set null on update cascade) engine=innodb; create table if not exists `shop` (`id` varchar(255) not null , `accesstoken` varchar(255) not null, `createdat` datetime not null, `updatedat` datetime not null, primary key (`id`)) engine=innodb;
but error
unhandled rejection sequelizedatabaseerror: er_cannot_add_foreign: cannot add foreign key constraint
when try see last foreign key error , says
------------------------ latest foreign key error ------------------------ 2016-07-28 19:23:21 0x700000d95000 error in foreign key constraint of table exitpopup/segments: foreign key (`wheelid`) references `wheel` (`id`) on delete set null on update cascade) engine=innodb: cannot resolve table name close to: (`id`) on delete set null on update cascade) engine=innodb
strangely, when put sql statements in sql console , works , there isn't error. doing wrong ?
the order needs change. creatig wheel
table before have created shop
table. wheel refers shop table not exists in original set of queries. when change order shop table exists error not occur.
create table if not exists `shop` (`id` varchar(255) not null , `accesstoken` varchar(255) not null, `createdat` datetime not null, `updatedat` datetime not null, primary key (`id`)) engine=innodb; create table if not exists `wheel` (`id` integer not null auto_increment , `createdat` datetime not null, `updatedat` datetime not null, `shopid` varchar(255), primary key (`id`), foreign key (`shopid`) references `shop` (`id`) on delete set null on update cascade) engine=innodb; create table if not exists `segments` (`segmentid` varchar(255) not null , `heading` varchar(255) not null, `subheading` varchar(255) not null, `createdat` datetime not null, `updatedat` datetime not null, `wheelid` integer, primary key (`segmentid`), foreign key (`wheelid`) references `wheel` (`id`) on delete set null on update cascade) engine=innodb;
Comments
Post a Comment