I’m getting this error when trying to add a foreign key contraint:
#1005 - Can't create table './testtable/#sql-595_146.frm' (errno: 150)
I need to do an ON DELETE CASCADE for all images that share a project id when that project is deleted. My simplified table structure is as follows:
CREATE TABLE IF NOT EXISTS `images` (
`image_id` mediumint(8) unsigned NOT NULL auto_increment,
`project_id` smallint(6) NOT NULL,
PRIMARY KEY (`image_id`),
KEY `project_id_ix` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
CREATE TABLE IF NOT EXISTS `projects` (
`project_id` smallint(5) unsigned NOT NULL auto_increment,
PRIMARY KEY (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
Now, When i run the query below to add the constraint, the query fails with the error posted above. Could anyone help?
ALTER TABLE `images` ADD CONSTRAINT `project_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects` (`project_id`) ON DELETE CASCADE;
Thanks a million!
Change the
project_idcolumns to have the same sizesmallint(6)(or 5). You also need to make them both signed or unsigned.…from the mysql website it says: