开发者

MYSQL - Cannot be able to create foreign key

开发者 https://www.devze.com 2022-12-12 10:37 出处:网络
When i execute the below query it throws an error, alter table `ttis`.`users` add constraint `FK_Role_ID` FOREIGN K开发者_运维技巧EY (`ROLE_ID`) REFERENCES `roles` (`ROLE_ID`)

When i execute the below query it throws an error,

alter table `ttis`.`users` 
add constraint `FK_Role_ID` FOREIGN K开发者_运维技巧EY (`ROLE_ID`) REFERENCES `roles` (`ROLE_ID`)

Error:

Error Code : 1005
Can't create table 'ttis.#sql-9c_8a' (errno: 150)


Have a look at this post: 13.6.4.4. FOREIGN KEY Constraints and search for "150". It says here:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to error 150, that means a foreign key definition would be incorrectly formed for the altered table. You can use SHOW ENGINE INNODB STATUS to display a detailed explanation of the most recent InnoDB foreign key error in the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消