0

我最近将一个系统迁移到一个新服务器并使用 InnoDB 表,我遇到了一些外键约束问题。大多数我修复了,但我被困在一个。系统(java webapp)总是自己处理外键的东西,所以这有点新,但只要我能正常工作就可以了。

有4个分层表macro_client、client、client_location和client_contact...所以一个macro_client可以有很多客户端等等。client_contact 有前 3 个的外键。还有一个 case 表,它们通过 m:n 表连接。

系统执行的一项任务是将新记录的一部分移动到现有记录中,例如将新的 client_contact 和 client_location 移动到先前存在的 client 和 macro_client 下。这是在处理提交并且有人发现系统中已经存在客户端时完成的 - 系统使这很容易。因此系统会更改 id 来执行此操作(然后删除悬空的客户端和不再需要的 macro_client)。这就是错误出现的地方。

这是我收到的错误。我一次在 MySQL Workbench 中手动运行这些更新语句,我得到了相同的消息。它发生在我第一次更新时,无论我尝试过哪种顺序。

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`prod1_cases_clients`.`client_contact_cases`, CONSTRAINT `fk_{345FCA29-D80B-48E0-A25E-06432436F9A6}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENC)

更新是

UPDATE client_contact SET macro_client_id = 61, client_id = 55 WHERE macro_client_id = 13368;
UPDATE client_location SET macro_client_id = 61, client_id = 55 WHERE macro_client_id = 13368;
UPDATE client_contact_cases SET macro_client_id = 61, client_id = 55 WHERE case_id = 1286148;

我要解决这个问题了吗?我需要更改外键行为吗?我更喜欢 webapp 处理外键更新,因为它已经这样做并保持可移植性。我只需要让它在我猜的外键心态下工作。否则我只需要每次都关闭和打开外键检查。或者也许这是我唯一的选择?

谢谢你的帮助! 在此处输入图像描述

更新的图像和添加的显示创建表您可以看到明显的圆圈,但它并不是一直一对多的。我可以删除 client_contact 和 package 之间的链接——它并没有真正被使用。

CREATE TABLE `macro_client` (...,
  PRIMARY KEY (`macro_client_id`)
)
CREATE TABLE `client` (...,
  PRIMARY KEY (`client_id`,`macro_client_id`),
  KEY `client_FKIndex1` (`macro_client_id`),
  KEY `index_client_referrer_form_url_name` (`referrer_form_url_name`),
  CONSTRAINT `fk_{3CBF0AA2-914E-49DF-930D-8DA10CA5DDA7}` FOREIGN KEY (`macro_client_id`) REFERENCES `macro_client` (`macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `client_location` (...,
  PRIMARY KEY (`client_loc_id`,`client_id`,`macro_client_id`),
  KEY `client_location_FKIndex1` (`client_id`,`macro_client_id`),
  CONSTRAINT `fk_{98D38761-65BA-4FC1-9BC9-2B6B95B23394}` FOREIGN KEY (`client_id`, `macro_client_id`) REFERENCES `client` (`client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `client_contact` (...,
  PRIMARY KEY (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
  UNIQUE KEY `weblogin_user_UNIQUE` (`weblogin_user`),
  KEY `client_contact_FKIndex1` (`client_loc_id`,`client_id`,`macro_client_id`),
  CONSTRAINT `fk_{B0C4B667-CDD6-4DEF-BC06-31C7C9BB29C2}` FOREIGN KEY (`client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_location` (`client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)


CREATE TABLE `client_contact_cases` (...,
  PRIMARY KEY (`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`,`case_id`),
  KEY `client_contact_has_cases_FKIndex1` (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
  KEY `client_contact_has_cases_FKIndex2` (`case_id`),
  CONSTRAINT `fk_{345FCA29-D80B-48E0-A25E-06432436F9A6}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_contact` (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_{1C2AE8A5-3D40-4827-99CD-AC1D3A2EE4C9}` FOREIGN KEY (`case_id`) REFERENCES `cases` (`case_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

CREATE TABLE `cases` (...,
  PRIMARY KEY (`case_id`),
  KEY `index_cases_case_mgr` (`case_mgr`),
  KEY `index_cases_status_code` (`status_code`)
)

CREATE TABLE `package` (...,
  PRIMARY KEY (`package_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
  KEY `package_FKIndex1` (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
  KEY `package_loc_id_index` (`loc_id`),
  KEY `package_package_logger_index` (`package_logger`),
  KEY `package_recd_sent_date_index` (`recd_sent_date`),
  CONSTRAINT `fk_{37F3D2D5-0D52-4199-B963-34FEE1AD3F2F}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_contact` (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1343 DEFAULT CHARSET=utf8 PACK_KEYS=0

CREATE TABLE `item` (...,
  PRIMARY KEY (`item_id`,`package_id`,`case_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
  KEY `item_FKIndex1` (`package_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
  KEY `item_FKIndex2` (`case_id`),
  CONSTRAINT `fk_{2FD3D784-5AFC-47A6-A836-5A421A603B6D}` FOREIGN KEY (`package_id`, `macro_client_id`, `client_id`, `client_loc_id`, `client_contact_id`) REFERENCES `package` (`package_id`, `macro_client_id`, `client_id`, `client_loc_id`, `client_contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_{30C849E2-6843-4815-8FD5-0F1C21C6976B}` FOREIGN KEY (`case_id`) REFERENCES `cases` (`case_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
4

0 回答 0