MYSQL: Cannot add foreign key constraint -
i've read many of questions talk problem none seem solve mine.
basically, receive following error:
error code: 1215. cannot add foreign key constraint
when trying execute following query:
create table if not exists privacy ( biography varchar(20), birthdate varchar(20), email varchar(20), location varchar(20), realname varchar(20), sex varchar(6), user varchar(255), primary key (user), foreign key (user) references users(username) on delete set null on update cascade ) character set utf8;
i thought couldn't have primary key , foreign on same column, made searches , found out it's fine. maybe i'm doing in wrong way.
edit:
this users table:
create table if not exists users (" avatar varchar(255), biography text, birth_date date, email varchar(255), location varchar(255), password varchar(20), profile_views int(11), real_name varchar(255), reputation int(11), signup_date datetime, username varchar(255), primary key (username) ) character set utf8;
the reason on delete set null on update cascade
line.
if use restrict mysql happy.
it makes sense, since can not make primary key null once delete user users table.
edit: can use on delete cascade
there pointed out in comments, delete entire row privacy.
Comments
Post a Comment