When I run the following command I receive a Error Code 1451. Cannon delete or update a parent row: a foreign key constraints fails
call databasename.DeleteFeatureCategory(36);
07:35:32 call databasename.DeleteFeatureCategory(36) Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`databasename`.`productFeature`, CONSTRAINT `fk_feature_prodCat` FOREIGN KEY (`productCategoryId`) REFERENCES `productCategory` (`id`)) 0.062 sec
This is the code that is being called:
CREATE DEFINER=`admin`@`x.x.x.x` PROCEDURE `DeleteFeatureCategory`(IN id INT)
BEGIN
DECLARE rowsAffected INT;
-- Attempt to delete the record
DELETE FROM `productCategory` WHERE `id` = id LIMIT 1;
-- Check how many rows were affected
SET rowsAffected = ROW_COUNT();
-- Return a response based on the number of affected rows
IF rowsAffected > 0 THEN
SELECT 'Product Category deleted successfully' AS response;
ELSE
SELECT 'Product Category not found' AS response;
END IF;
END
When I run the command outside of the stored proc it is successful
DELETE FROM
productCategoryWHERE
id = 35 LIMIT 1;
07:34:47 DELETE FROM `productCategory` WHERE `id` = 35 LIMIT 1 1 row(s) affected 0.078 sec
Here is the table that row is attempting to be removed from:
CREATE TABLE `productCategory` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8mb4_general_ci DEFAULT NULL,
`description` varchar(45) COLLATE utf8mb4_general_ci DEFAULT NULL,
`productLineId` int DEFAULT NULL,
`productId` int DEFAULT NULL,
`productCategoryTypeId` int DEFAULT NULL,
`createdBy` int DEFAULT NULL,
`createdOn` datetime DEFAULT CURRENT_TIMESTAMP,
`modifiedBy` int DEFAULT NULL,
`modifiedOn` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fs_prodCat_prodLine_idx` (`productLineId`),
KEY `fk_prodCat_product_idx` (`productId`),
CONSTRAINT `fk_prodCat_prodLine` FOREIGN KEY (`productLineId`) REFERENCES `productLine` (`id`),
CONSTRAINT `fk_prodCat_product` FOREIGN KEY (`productId`) REFERENCES `product` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Here is the table that has the constraints:
CREATE TABLE `productFeature` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8mb4_general_ci DEFAULT NULL,
`description` varchar(250) COLLATE utf8mb4_general_ci DEFAULT NULL,
`productId` int DEFAULT NULL,
`productCategoryId` int DEFAULT NULL,
`createdBy` int DEFAULT NULL,
`createOn` datetime DEFAULT CURRENT_TIMESTAMP,
`modifiedBy` int DEFAULT NULL,
`modifiedOn` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_feature_prodCat_idx` (`productCategoryId`),
KEY `fk_feature_product_idx` (`productId`),
CONSTRAINT `fk_feature_prodCat` FOREIGN KEY (`productCategoryId`) REFERENCES `productCategory` (`id`),
CONSTRAINT `fk_feature_product` FOREIGN KEY (`productId`) REFERENCES `product` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Column A | Column B |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
Current productFeature information:
| id | name | description | productId | productCategoryId | createdBy | createOn | modifiedBy | modifiedOn
1 | IPv4 | Internet Protocol Version 4 | 1 | 1 | 1 | 2024-07-19 15:08:48 | 1 | 2024-07-19 15:08:48
2 | M-100 | Panorama M-100 | 2 | 7 | 1 | 2024-07-21 14:18:03 | 1 | 2024-07-21 14:18:03
3 | PA-220 | PA-220 Series Firewall | 3 | 2 | 1 | 2024-07-21 14:20:32 | 1 | 2024-07-21 14:20:32
4 | M-700 | Panorama M-700 | 2 | 7 | 1 | 2024-07-21 14:38:51 | 1 | 2024-07-21 14:38:51
5 | PA-3220 | PA-3220 Series Firewall | 3 | 2 | 1 | 2024-07-21 14:38:51 | 1 | 2024-07-21 14:38:51
6 | NCP | Network Control Plane | 3 | 8 | 1 | 2024-08-11 06:35:10 | 1 | 2024-08-11 06:35:10
7 | test | test | 82 | 32 | 1 | 2024-08-16 04:36:12 | 1 | 2024-08-16 04:36:12
8 | 004 test | Wow!! | 82 | 32 | 1 | 2024-08-16 04:40:32 | 1 | 2024-08-16 04:40:32
9 | 004 123 | Cloud Stuff | 82 | 32 | 1 | 2024-08-16 04:42:06 | 1 | 2024-08-16 04:42:06
10 | 004 test again | testing 123 | 82 | 34 | 1 | 2024-08-16 04:46:10 | 1 | 2024-08-16 04:46:10
Workbench stated mySQL version 8.0.32-0ubuntu0.20.04.2 ((Ubuntu))
Do Stored Procs and constraints need to be handled differently?
Gregory Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1