php - Why won't my foreign key create in MySQL? -


i've tried many different ways create table foreign key , trying insert phpmyadmin. however, not working expected.

here i've far:

create table user (     user_id bigint(10) unsigned auto_increment primary key,      user_name varchar(50) not null,     user_password varchar(50) not null); 

this works fine. however, if try add table foreign key thus, refuses create:

create table article (     article_id int(20) unsigned auto_increment primary key,      article_title varchar(100) not null,     article_content varchar(1000) not null,     user_id int(10) not null,     foreign key (user_id) references user (user_id)); 

this not work expected , not add table mysql database. error:

cannot add foreign key constraint

how can fix it?

we discovered in comments if primary key defined thus:

user_id bigint(10) unsigned 

then foreign key not work, since needs match on signedness (and think type too):

user_id int(10) not null 

this works fine:

user_id bigint(10) unsigned not null 

here's fiddle demonstrate working.


Popular posts from this blog