Question

When i execute the below query it throws an error,

alter table `ttis`.`users` 
add constraint `FK_Role_ID` FOREIGN KEY (`ROLE_ID`) REFERENCES `roles` (`ROLE_ID`)

Error:

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

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top