是从第一个触发器启动的第二个触发器引起错误吗?

时间:2019-04-12 07:41:35

标签: mysql database-trigger mysql-error-1442

我正在使用一个名为inbox的表,该表存储来自skills_matrix的更改skills_matrixarea_id的请求。 skills_matrix上的触发器执行正常,但是在更新inbox时,skills_matrix上的触发器被中断,出现以下错误:

ERROR 1442 (HY000): Can't update table 'inbox' in stored function/trigger because it 
is already used by statement which invoked this stored function/trigger

我尝试添加额外的列skills_matrixarea_change_pending以在skills_matrix触发条件中使用以防止语句执行。

CREATE DEFINER=`root`@`localhost` TRIGGER `skills_matrix_BEFORE_UPDATE` BEFORE UPDATE ON `skills_matrix` FOR EACH ROW BEGIN
    IF (OLD.area_change_pending = 0 AND OLD.area_id != NEW.area_id) THEN
        INSERT INTO `inbox` (`id`, `prev_area`, `prop_area`, `status`) VALUES ("0",OLD.area_id,NEW.area_id,"0");
        SET NEW.area_id = OLD.area_id;
        SET NEW.area_change_pending = 1;
    END IF;
END

也在网上搜索:

SQL triggers cause errors

Triggers logic causes error

其余的SQL:

| skill_matrix |

CREATE TABLE `skills_matrix` (
`id` int(11) NOT NULL,
`area_id` int(11) NOT NULL,
`area_change_pending` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0 = Not pending request, 1 = pending request to change area',
PRIMARY KEY (`id`),
KEY `fk_skills_matrix_area1_idx` (`area_id`)
) 

ENGINE=InnoDB DEFAULT CHARSET=utf8 |

CREATE DEFINER=`root`@`localhost` TRIGGER `skills_matrix_BEFORE_UPDATE` BEFORE UPDATE ON `skills_matrix` FOR EACH ROW BEGIN
    IF (OLD.area_change_pending = 0 AND OLD.area_id != NEW.area_id) THEN
        INSERT INTO `inbox` (`id`, `prev_area`, `prop_area`, `status`) VALUES ("0",OLD.area_id,NEW.area_id,"0");
        SET NEW.area_id = OLD.area_id;
        SET NEW.area_change_pending = 1;
    END IF;
END

|收件箱|

CREATE TABLE `inbox` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`prev_area` int(11) DEFAULT NULL,
`prop_area` int(11) DEFAULT NULL,
`status` tinyint(1) DEFAULT 0 COMMENT '0 = pending acceptance, 1 = accepted',
PRIMARY KEY (`id`,`clock_number`)
) 

ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 |

CREATE DEFINER=`root`@`localhost` TRIGGER `inbox_BEFORE_UPDATE` BEFORE UPDATE ON `inbox` FOR EACH ROW BEGIN
    IF (NEW.status = 1) THEN
    UPDATE `skills_matrix`  SET `skills_matrix`.`area_change_pending` = 0, `skills_matrix`.`area_id` = NEW.prop_area;
    DELETE FROM `loadall_portal`.`skills_matrix` WHERE `loadall_portal`.`skills_matrix`.`status` = 1;
    END IF;
END

可以预期的是,当inboxstatus更改为1时,它将用{{1}更新skills_matrixarea_id。 }。NEW,然后从prop_area删除行并设置inboxskills_matrix area_change_pending,因为现在已接受区域更改。

即使在= 0条件下OLDarea_change_pending阻止skills_matrix_BEFORE_UPDATE触发时执行任何语句,为什么仍显示此错误?有一个更好的方法吗?我使用触发器是否正确,还是可以使用更简单的方法来实现? (首次使用触发器-非常感谢您的帮助!)

0 个答案:

没有答案