Ok so we get this error when creating foreign key . So what now ?
create table users (
id_user int(11) unsigned not null primary key auto_increment,
name varchar(64)
)Engine=InnoDB;
insert into users set name='Admin';
create table test_relation (
id int(11) unsigned NOT NULL primary key AUTO_INCREMENT,
id_user int(10) not null )
engine=InnoDB;
alter table test_relation add foreign key(id_user) references users(id_user);
Can't create table 'test.#sql-440_ba' (errno: 150)
This message says complete nothing ; )
To get more detailed information use
mysql> show innodb status;
and locate "LATEST FOREIGN KEY ERROR" part.
120717 15:39:19 Error in foreign key constraint of table test/#sql-440_ba:
foreign key(id_user) references test.users(id_user):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Yeah, so we got info that there is something wrong with column types.
Quick debug:
mysql> alter table test_relation modify id_user int(10) unsigned not null;
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test.test_relation add foreign key(id_user) references test.users(id_user);
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0
and voila works perfect ; )
Brak komentarzy:
Prześlij komentarz