mysql - Why am I getting "Cannot add foreign key constraint" error with the following sql command? -


i getting error while running following commands on mysql server.

create table users (     userid int not null auto_increment,     primary key(userid),     firstname varchar(100) not null,     lastname varchar(100) not null,     username varchar(120) not null unique,     hashpass varchar(100) not null ) engine=innodb ;   create table posts (     postid int auto_increment,     text varchar(1000),     imageuri varchar(100),     userid varchar(120) not null,       primary key(postid),      foreign key (userid)     references users (userid)     on delete cascade,      tags varchar(500) ) engine=innodb; 

i getting following error:

cannot add foreign key constraint 

try use same type of primary keys - users.userid int - on foreign keys - posts.userid - well.

working sql fiddle

try this:

create table users ( userid int not null auto_increment, primary key(userid), firstname varchar(100) not null, lastname varchar(100) not null, username varchar(120) not null unique, hashpass varchar(100) not null ) engine=innodb ;   create table posts (  postid int auto_increment, text varchar(1000), imageuri varchar(100), userid int not null,     <---change here  primary key(postid),  foreign key (userid) references users (userid) on delete cascade,  tags varchar(500) ) engine=innodb; 

Comments