创建外键时出错

发布于 2024-08-16 09:30:59 字数 877 浏览 8 评论 0原文

尝试添加外键约束时出现此错误:

#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;

谢谢一百万!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救星 2024-08-23 09:30:59

project_id 列更改为具有相同大小的 smallint(6)(或 5)。您还需要对它们进行签名或未签名。

...来自 mysql 网站它说:

外键和引用键中的相应列在 InnoDB 中必须具有相似的内部数据类型,以便可以在不进行类型转换的情况下进行比较。整数类型的大小和符号必须相同。字符串类型的长度不必相同。对于非二进制(字符)字符串列,字符集和排序规则必须相同。

Change the project_id columns to have the same size smallint(6) (or 5). You also need to make them both signed or unsigned.

...from the mysql website it says:

Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

找回味觉 2024-08-23 09:30:59

我认为数据类型需要匹配。一张表中的project_id 为smallint(6),另一张表中的project_id 为smallint(5)。

I thought the datatype needed to match. project_id is smallint(6) in one table and smallint(5) in the other.

黒涩兲箜 2024-08-23 09:30:59

project_id 不应该都是 smallint(6) 吗?

Shouldn't the project_id's both be smallint(6)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文