为什么“外键约束失败”外键何时存在?

发布于 2024-09-26 15:13:19 字数 4524 浏览 6 评论 0原文

我有一个简单的查询,

UPDATE `t_timecard_detail` SET  `timeoff_request_id` = 'adad8e0d-c22b-41c3-a460-6cf982729299' WHERE `id` = 'cfc7a0a1-4e03-46a4-af89-069a0661cf55'; 

它给出了这个错误

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demo1_timeapp`.`t_timecard_detail`, CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`))

约束,

CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)

虽然ID'adad8e0d-c22b-41c3-a460-6cf982729299'存在于t_timeoff_request表中

mysql> select `id`  from t_timeoff_request where `id`='adad8e0d-c22b-41c3-a460-6cf982729299';
+--------------------------------------+
| id                                   |
+--------------------------------------+
| adad8e0d-c22b-41c3-a460-6cf982729299 |
+--------------------------------------+

所以我不确定为什么当外键存在时mysql说'外键约束失败'?

mysql 版本:

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (i486) using readline 6.1

编辑:看起来一个表有 ENGINE=MyISAM 和

show create table t_timecard_detail;

CREATE TABLE `t_timecard_detail` (
  `id` varchar(36) NOT NULL,
  `change_time` datetime NOT NULL,
  `department_id` varchar(36) DEFAULT NULL,
  `day` date NOT NULL,
  `in_punch_id` varchar(36) DEFAULT NULL,
  `punch_in` datetime DEFAULT NULL,
  `paid_in` datetime DEFAULT NULL,
  `infraction_in_id` varchar(36) DEFAULT NULL,
  `schedule_in` datetime DEFAULT NULL,
  `type_in` varchar(10) NOT NULL,
  `out_punch_id` varchar(36) DEFAULT NULL,
  `punch_out` datetime DEFAULT NULL,
  `paid_out` datetime DEFAULT NULL,
  `schedule_out` datetime DEFAULT NULL,
  `infraction_out_id` varchar(36) DEFAULT NULL,
  `type_out` varchar(10) NOT NULL,
  `work_min` int(11) NOT NULL,
  `ot_min` int(11) NOT NULL,
  `ot2_min` int(11) NOT NULL,
  `pay_type_id` varchar(36) NOT NULL,
  `timecard_id` varchar(36) NOT NULL,
  `user_entered` tinyint(1) NOT NULL,
  `comments` varchar(256) DEFAULT NULL,
  `timeoff_request_id` varchar(36),
  PRIMARY KEY (`id`),
  KEY `t_timecard_detail_department_id` (`department_id`),
  KEY `t_timecard_detail_in_punch_id` (`in_punch_id`),
  KEY `t_timecard_detail_infraction_in_id` (`infraction_in_id`),
  KEY `t_timecard_detail_out_punch_id` (`out_punch_id`),
  KEY `t_timecard_detail_infraction_out_id` (`infraction_out_id`),
  KEY `t_timecard_detail_pay_type_id` (`pay_type_id`),
  KEY `t_timecard_detail_timecard_id` (`timecard_id`),
  KEY `t_timecard_detail_4f5ffbb5` (`timeoff_request_id`),
  CONSTRAINT `department_id_refs_id_1b23ee35` FOREIGN KEY (`department_id`) REFERENCES `t_department` (`id`),
  CONSTRAINT `infraction_in_id_refs_id_17b2e173` FOREIGN KEY (`infraction_in_id`) REFERENCES `t_pay_group_infraction` (`id`),
  CONSTRAINT `infraction_out_id_refs_id_17b2e173` FOREIGN KEY (`infraction_out_id`) REFERENCES `t_pay_group_infraction` (`id`),
  CONSTRAINT `in_punch_id_refs_id_4d13a8b2` FOREIGN KEY (`in_punch_id`) REFERENCES `t_punch` (`id`),
  CONSTRAINT `out_punch_id_refs_id_4d13a8b2` FOREIGN KEY (`out_punch_id`) REFERENCES `t_punch` (`id`),
  CONSTRAINT `pay_type_id_refs_id_70cb7404` FOREIGN KEY (`pay_type_id`) REFERENCES `t_pay_type` (`id`),
  CONSTRAINT `timecard_id_refs_id_7889236c` FOREIGN KEY (`timecard_id`) REFERENCES `t_timecard` (`id`),
  CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

其他 innodb输出 show create table t_timeoff_request; 的输出

CREATE TABLE `t_timeoff_request` (
  `id` varchar(36) NOT NULL,
  `change_time` datetime NOT NULL,
  `employee_id` varchar(36) NOT NULL,
  `submitted_date` datetime NOT NULL,
  `time_off_day` date NOT NULL,
  `mins_charged` int(11) NOT NULL,
  `pay_type_id` varchar(36) NOT NULL,
  `employee_comment` varchar(300) NOT NULL,
  `approved_status` varchar(10) NOT NULL,
  `approved_by_id` varchar(36) DEFAULT NULL,
  `approved_date` datetime DEFAULT NULL,
  `approved_comment` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `t_timeoff_request_employee_id` (`employee_id`),
  KEY `t_timeoff_request_pay_type_id` (`pay_type_id`),
  KEY `t_timeoff_request_approved_by_id` (`approved_by_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

I have a simple query

UPDATE `t_timecard_detail` SET  `timeoff_request_id` = 'adad8e0d-c22b-41c3-a460-6cf982729299' WHERE `id` = 'cfc7a0a1-4e03-46a4-af89-069a0661cf55'; 

which gives this error

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demo1_timeapp`.`t_timecard_detail`, CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`))

constraint is

CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)

though the ID 'adad8e0d-c22b-41c3-a460-6cf982729299' exists in t_timeoff_request table

mysql> select `id`  from t_timeoff_request where `id`='adad8e0d-c22b-41c3-a460-6cf982729299';
+--------------------------------------+
| id                                   |
+--------------------------------------+
| adad8e0d-c22b-41c3-a460-6cf982729299 |
+--------------------------------------+

So I am not sure why mysql says 'foreign key constraint fails' when the foreign key exists?

mysql version:

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (i486) using readline 6.1

Edit: Looks like one table has ENGINE=MyISAM and other innodb

output of show create table t_timecard_detail;

CREATE TABLE `t_timecard_detail` (
  `id` varchar(36) NOT NULL,
  `change_time` datetime NOT NULL,
  `department_id` varchar(36) DEFAULT NULL,
  `day` date NOT NULL,
  `in_punch_id` varchar(36) DEFAULT NULL,
  `punch_in` datetime DEFAULT NULL,
  `paid_in` datetime DEFAULT NULL,
  `infraction_in_id` varchar(36) DEFAULT NULL,
  `schedule_in` datetime DEFAULT NULL,
  `type_in` varchar(10) NOT NULL,
  `out_punch_id` varchar(36) DEFAULT NULL,
  `punch_out` datetime DEFAULT NULL,
  `paid_out` datetime DEFAULT NULL,
  `schedule_out` datetime DEFAULT NULL,
  `infraction_out_id` varchar(36) DEFAULT NULL,
  `type_out` varchar(10) NOT NULL,
  `work_min` int(11) NOT NULL,
  `ot_min` int(11) NOT NULL,
  `ot2_min` int(11) NOT NULL,
  `pay_type_id` varchar(36) NOT NULL,
  `timecard_id` varchar(36) NOT NULL,
  `user_entered` tinyint(1) NOT NULL,
  `comments` varchar(256) DEFAULT NULL,
  `timeoff_request_id` varchar(36),
  PRIMARY KEY (`id`),
  KEY `t_timecard_detail_department_id` (`department_id`),
  KEY `t_timecard_detail_in_punch_id` (`in_punch_id`),
  KEY `t_timecard_detail_infraction_in_id` (`infraction_in_id`),
  KEY `t_timecard_detail_out_punch_id` (`out_punch_id`),
  KEY `t_timecard_detail_infraction_out_id` (`infraction_out_id`),
  KEY `t_timecard_detail_pay_type_id` (`pay_type_id`),
  KEY `t_timecard_detail_timecard_id` (`timecard_id`),
  KEY `t_timecard_detail_4f5ffbb5` (`timeoff_request_id`),
  CONSTRAINT `department_id_refs_id_1b23ee35` FOREIGN KEY (`department_id`) REFERENCES `t_department` (`id`),
  CONSTRAINT `infraction_in_id_refs_id_17b2e173` FOREIGN KEY (`infraction_in_id`) REFERENCES `t_pay_group_infraction` (`id`),
  CONSTRAINT `infraction_out_id_refs_id_17b2e173` FOREIGN KEY (`infraction_out_id`) REFERENCES `t_pay_group_infraction` (`id`),
  CONSTRAINT `in_punch_id_refs_id_4d13a8b2` FOREIGN KEY (`in_punch_id`) REFERENCES `t_punch` (`id`),
  CONSTRAINT `out_punch_id_refs_id_4d13a8b2` FOREIGN KEY (`out_punch_id`) REFERENCES `t_punch` (`id`),
  CONSTRAINT `pay_type_id_refs_id_70cb7404` FOREIGN KEY (`pay_type_id`) REFERENCES `t_pay_type` (`id`),
  CONSTRAINT `timecard_id_refs_id_7889236c` FOREIGN KEY (`timecard_id`) REFERENCES `t_timecard` (`id`),
  CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

output of show create table t_timeoff_request;

CREATE TABLE `t_timeoff_request` (
  `id` varchar(36) NOT NULL,
  `change_time` datetime NOT NULL,
  `employee_id` varchar(36) NOT NULL,
  `submitted_date` datetime NOT NULL,
  `time_off_day` date NOT NULL,
  `mins_charged` int(11) NOT NULL,
  `pay_type_id` varchar(36) NOT NULL,
  `employee_comment` varchar(300) NOT NULL,
  `approved_status` varchar(10) NOT NULL,
  `approved_by_id` varchar(36) DEFAULT NULL,
  `approved_date` datetime DEFAULT NULL,
  `approved_comment` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `t_timeoff_request_employee_id` (`employee_id`),
  KEY `t_timeoff_request_pay_type_id` (`pay_type_id`),
  KEY `t_timeoff_request_approved_by_id` (`approved_by_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

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

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

发布评论

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

评论(3

攀登最高峰 2024-10-03 15:13:19

我认为这是一个错误。
MySQL 不应该允许您从 InnoDB 添加外键引用到 MyISAM 表,因为 MyISAM 引擎不是事务性的,而 InnoDb 是。

Try:
ALTER TABLE t_timeoff_request ENGINE=InnoDB;

I think it's a bug.
MySQL shouldn't allow You to add FOREIGN KEY referencing to MyISAM table from InnoDB because MyISAM engine isn't transactional, InnoDb is.

Try:
ALTER TABLE t_timeoff_request ENGINE=InnoDB;
一个人的旅程 2024-10-03 15:13:19

外键约束不能应用于MyISAM引擎。两个表都应该使用innodb引擎来支持外键约束。

Foreign key constraints can't be applied to MyISAM engine.both table should use innodb engine for supporting foreign key constraints.

痴骨ら 2024-10-03 15:13:19

如果您对 FK 设置约束,则问题可能出在另一个表中。
您是否设置了 FK 选项,例如 ON UPDATE CASCADE?

Maybe the problem is in another table, if you set a constraint on the FK.
Did you set up a FK option, like ON UPDATE CASCADE?

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