mysql error 1005 errno 121 when trying to set a foreign key



There is a problem while creating a foreign key to a table with the mysql. So foreign key includes an extension like fk_menu_id_idbfk1 as the key name.

Create the foreign key like this whenever you faced this error.



DROP TABLE IF EXISTS `as_menu_action`;
CREATE TABLE IF NOT EXISTS `as_menu_action` (
  `id` tinyint(2) NOT NULL auto_increment,
  `menu_id` tinyint(2) NOT NULL,
  `action_name` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `menu_id` (`menu_id`), 
  CONSTRAINT FOREIGN KEY (`menu_id`) REFERENCES `as_menu` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;