尝试添加外键约束时出现此错误:
#1005 - Can't create table './testtable/#sql-595_146.frm' (errno: 150)
在删除该项目时,我需要对共享项目 ID 的所有图像执行 ON DELETE CASCADE。我的简化表结构如下:
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 ;
现在,当我运行下面的查询以添加约束时,查询失败并出现上面发布的错误。有人可以帮忙吗?
ALTER TABLE `images` ADD CONSTRAINT `project_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects` (`project_id`) ON DELETE CASCADE;
太感谢了!