我正在学习 PHP/MySQL 并遇到了这个。 我梳理了 Stack Overflow,但没有找到任何可以帮助我解决问题的内容。
它在 PHP 7.4.27 / MySQL 8.0.21 下运行。
我最初的问题是用截断的表格发布的,以缩短帖子的长度。 我已经用完整的表描述更新了它,并添加了我正在使用的确切查询。
我得到:
SQLSTATE[23000]:违反完整性约束:1451 无法删除或更新父行:外键约束失败(`evifs_aqqbo`.`aq_customers`,CONSTRAINT `fk_cust_proj` FOREIGN KEY (`project_id`) REFERENCES `aq_project` (` project_id`) 删除级联)
将新记录插入到
aq_project
表中时。
aq_项目表:
CREATE TABLE `aq_customers` (
`customer_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`parent_id` int DEFAULT NULL,
`qb_Customer_id` int DEFAULT NULL,
`customer_address_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_specific_sales_tax` decimal(5,2) DEFAULT NULL,
`customer_specific_markup` decimal(5,2) DEFAULT NULL,
`export_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`jobref` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`project_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`jobref_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`customer_id`,`project_id`,`jobref_code`),
KEY `indx_custs_cust_addrid` (`customer_address_id`),
KEY `indx_li_sli` (`project_id`),
CONSTRAINT `fk_cust_proj` FOREIGN KEY (`project_id`) REFERENCES `aq_project` (`project_id`) ON DELETE CASCADE,
CONSTRAINT `fk_custs_cust_addrid` FOREIGN KEY (`customer_address_id`) REFERENCES `aq_addressrepository` (`address_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
aq_customers 表:
CREATE TABLE `aq_project` (
`project_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`status` enum('Active','Inactive') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`jobref` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`jobref_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`project_name` varchar(75) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`contract_match` int DEFAULT NULL,
`parent_id` int DEFAULT NULL,
`aq_dealer_rep` int DEFAULT '0',
`create_date` date DEFAULT NULL,
`modified_date` date DEFAULT NULL,
`bid_date` date DEFAULT NULL,
`good_until_date` date DEFAULT NULL,
`project_address_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`freight_sell_override` decimal(10,2) DEFAULT NULL,
`installation_sell_override` decimal(10,2) DEFAULT NULL,
`freight_taxable` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`installation_taxable` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`locksell` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sales_tax_percent` decimal(5,2) DEFAULT NULL,
`sales_tax_override` decimal(5,2) DEFAULT NULL,
`marketing_category` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`read_only` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`read_only_description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password_protected` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`custom_filter` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`memo` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`opportunity_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`converted_prices_currency_symbol` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`converted_sell_price` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`converted_prices_currency_exchange_rate` decimal(6,3) DEFAULT NULL,
`custom_column_3_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`custom_column_1_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`custom_column_2_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date_db_modified` datetime DEFAULT CURRENT_TIMESTAMP,
`warehouse` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`duplicated_projects` int DEFAULT NULL,
`install_date` date DEFAULT NULL,
`isService` tinyint(1) DEFAULT '0',
`track_proj` enum('Yes','No') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`do_not_track_proj` tinyint DEFAULT NULL,
`qbo_customer_status` enum('true','false') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`dealer_rep_address_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`project_coordinator_address_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`dealer_rep` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`class_id` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_address_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parent_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`project_id`,`jobref_code`),
UNIQUE KEY `INDX_jobref_code` (`jobref_code`),
KEY `indx_proj_jobref` (`jobref`),
KEY `indx_proj_project_addrid` (`project_address_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
引发错误的查询是:
INSERT INTO aq_project (`code`,`create_date`,`password_protected`,`read_only_description`
,`read_only`,`sales_tax_percent`,`installation_taxable`,`freight_taxable`
,`freight_sell_override`,`project_address_id`,`good_until_date`,`bid_date`
,`modified_date`,`project_name`,`jobref`,`project_id`
,`status`,`locksell`,`custom_column_1_name`,`track_proj`
,`jobref_code`,`dealer_rep`,`dealer_rep_address_id`,`class_id`
,`project_coordinator_address_id`
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) as NEW
ON DUPLICATE KEY UPDATE `code` = new.`code`, `create_date` = new.`create_date`
, `password_protected` = new.`password_protected`
, `read_only_description` = new.`read_only_description`
, `read_only` = new.`read_only`
, `sales_tax_percent` = new.`sales_tax_percent`
, `installation_taxable` = new.`installation_taxable`
, `freight_taxable` = new.`freight_taxable`
, `freight_sell_override` = new.`freight_sell_override`
, `project_address_id` = new.`project_address_id`
, `good_until_date` = new.`good_until_date`
, `bid_date` = new.`bid_date`
, `modified_date` = new.`modified_date`
, `project_name` = new.`project_name`
, `jobref` = new.`jobref`
, `project_id` = new.`project_id`
, `status` = new.`status`
, `locksell` = new.`locksell`
, `custom_column_1_name` = new.`custom_column_1_name`
, `track_proj` = new.`track_proj`
, `jobref_code` = new.`jobref_code`
, `dealer_rep` = new.`dealer_rep`
, `dealer_rep_address_id` = new.`dealer_rep_address_id`
, `class_id` = new.`class_id`
, `project_coordinator_address_id` = new.`project_coordinator_address_id`
;
哪里
$values = array (
0 => 'CONTRACT',
1 => '2024-12-10',
2 => 'true',
3 => 'Not Read Only',
4 => '0',
5 => '7',
6 => 'true',
7 => 'true',
8 => '1200',
9 => '15e149c4-0f04-49e1-8761-d70ede7332f6',
10 => '2025-01-09T12:27:54.6841706',
11 => '2024-12-10T12:27:54.6841706',
12 => '2024-12-11',
13 => '242011 Tom\'s Sub Shop Charles Town, WV',
14 => '242011',
15 => '3f7265f4-57b3-40d5-b4dd-ceaad93ae4a6',
16 => 'Active',
17 => '',
18 => 'Water AFF',
19 => 'Yes',
20 => '242011_CONTRACT',
21 => 'Long L',
22 => 'b159136f-e9a7-4e15-8781-7a92929ddb03',
23 => '4100000000000368102',
24 => 'e0c456ff-655c-4c9a-99a3-3f567ed2b16f',
)
project_id 是数组中的第 15 个元素:'3f7265f4-57b3-40d5-b4dd-ceaad93ae4a6`
aq_customers
引用aq_project
,所以我不明白为什么我不能在父表中插入记录而不引发此违规。 aq_customers
中的fk用于在删除aq_customers
中的项目时,级联删除aq_project
中的项目记录。
我将给出OP所示错误发生情况的简化模型。
当从 aq_Customers 表通过 Project_id 列的链接需要 aq_Project 表中具有此类 Project_id 的行时,两个表存在约束。
如果您尝试更新“项目”表中的一行以使来自“客户”的链接变得无效,则会出现指定的错误。
1451 无法删除或更新父行:外键约束失败
参见示例
CREATE TABLE `aq_project` (
`project_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`status` enum('Active','Inactive','Test') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`jobref` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`jobref_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`project_name` varchar(75) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`project_id`,`jobref_code`),
UNIQUE KEY `INDX_jobref_code` (`jobref_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `aq_customers` (
`customer_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`parent_id` int DEFAULT NULL,
`customer_address_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`project_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`jobref_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`customer_id`,`project_id`,`jobref_code`),
CONSTRAINT `fk_cust_proj` FOREIGN KEY (`project_id`) REFERENCES `aq_project` (`project_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO aq_project VALUES
('project_id1','Active','jobref1','code1','jobref code1','project name1');
SELECT * FROM aq_project;
项目_id | 状态 | 工作参考 | 代码 | 工作参考代码 | 项目名称 |
---|---|---|---|---|---|
项目_id1 | 活跃 | 工作参考1 | 代码1 | 工作参考代码1 | 项目名称1 |
INSERT INTO aq_project VALUES
('project_id1','Active','jobref1','code1','jobref code2','project name1');
项目_id | 状态 | 工作参考 | 代码 | 工作参考代码 | 项目名称 |
---|---|---|---|---|---|
项目_id1 | 活跃 | 工作参考1 | 代码1 | 工作参考代码1 | 项目名称1 |
项目_id1 | 活跃 | 工作参考1 | 代码1 | 工作参考代码2 | 项目名称1 |
INSERT INTO aq_customers VALUES
('customer_id1', null,'address_id1','project_id1','jobref code1')
;
本次DML操作成功。
客户 ID | parent_id | 客户地址_id | 项目_id | 工作参考代码 |
---|---|---|---|---|
客户_id1 | 空 | 地址_id1 | 项目_id1 | 工作参考代码1 |
下一个查询违反更新约束
project_id
= new.Project_id
INSERT INTO aq_project VALUES
('project_id2','Active','jobref1','code1','jobref code2','project name1') as new
ON DUPLICATE KEY UPDATE `status` = 'Test',`project_id`= new.Project_id
;
Cannot delete or update a parent row: a foreign key constraint fails (`fiddle`.`aq_customers`, CONSTRAINT `fk_cust_proj` FOREIGN KEY (`project_id`) REFERENCES `aq_project` (`project_id`) ON DELETE CASCADE)