In phpMyAdmin when i try to add foreign key some error occurs and does not tell what is wrong. Just says “FK fails”.
Can any one tell what is the problem?
Error
SQL query:
ALTER TABLE `hotel` ADD FOREIGN KEY ( `type_id` ) REFERENCES `hotel`.`hotel_type` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`hotel`.`#sql-cfc_e`, CONSTRAINT `#sql-cfc_e_ibfk_2` FOREIGN KEY (`type_id`) REFERENCES `hotel_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Be sure the columns of both tables are of the same data-type:
If they are not of the same type then you won’t be able to add your FK constraint.
— Edit —
Based on your response, the columns are the same data-types, so that means you have an invalid value in the
hotels.type_idcolumn (value doesn’t exist in thehotel_typestable). Check the values in yourhotels.type_idcolumn and make sure they exist in yourhotel_types.idcolumn.