我有某种继承的情况:
设备-> raut_controller-> raut_flat_temperature_control_controller
我创建了触发器来插入父行,以便在插入继承的表之前可以先获得其ID。在插入“ raut_controller”之前,触发器在device
表中插入行,使用LAST_INSERT_ID()获取ID并更改NEW.id。与raut_flat_temperature_control_controller
相同。因此,在raut_flat_temperature_control_controller中插入应该在3个表中调用经修改的插入。
问题是由于失败了外键约束,我无法在表raut_flat_temperature_control_controller
中插入。毕竟触发它尝试插入到id = 1的“ raut_flat_temperature_control_controller”中。在父表“ raut_controller”中不存在。在raut_controller
中触发时,已成功在device
表中插入行。
如果raut_flat_temperature_control_controller
为何LAST_INSERT_ID()在触发器中不起作用?
UPD。有什么办法可以忽略mysql中的BEFORE INSERT TRIGGER
我的桌子:
CREATE TABLE `device` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_type_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_device_type_id` (`device_type_id`),
CONSTRAINT `fk_device_type_id` FOREIGN KEY (`device_type_id`) REFERENCES `device_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE `raut_controller` (
`id` int(11) NOT NULL,
`raut_controller_type_id` int(11) NOT NULL,
`ip_adress` int(10) unsigned NOT NULL DEFAULT '0',
`mac_address` bigint(20) unsigned NOT NULL DEFAULT '0',
`serial_num` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_raut_controller_type_id_idx` (`raut_controller_type_id`),
CONSTRAINT `fk_raut_controller_device` FOREIGN KEY (`id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_raut_controller_type_id` FOREIGN KEY (`raut_controller_type_id`) REFERENCES `raut_controller_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE `raut_flat_temperature_control_controller` (
`id` int(11) NOT NULL,
`version` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_raut_flat_temperature_control` FOREIGN KEY (`id`) REFERENCES `raut_controller` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
和2个查询表以获取* type_id:
CREATE TABLE `device_type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`table_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unq_device_type_name` (`name`),
UNIQUE KEY `unq_device_type_table_name` (`table_name`),
KEY `idx_device_type_name` (`name`),
KEY `idx_device_type_table_name` (`table_name`)
);
CREATE TABLE `raut_controller_type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`table_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unq_raut_controller_type_name` (`name`),
UNIQUE KEY `unq_raut_controller_type_table_name` (`table_name`),
KEY `idx_raut_controller_type_name` (`name`),
KEY `id_raut_controller_type_table_name` (`table_name`)
);
触发器
CREATE TRIGGER `raut_controller_device_id` BEFORE INSERT ON `raut_controller` FOR EACH ROW
BEGIN
INSERT INTO `reeve`.`device` (
`device_type_id`
)
VALUES (
(SELECT `id` FROM `reeve`.`device_type` WHERE table_name = 'raut_controller')
);
SET NEW.id = LAST_INSERT_ID();
END
CREATE TRIGGER `raut_flat_temperature_control_controller_id` BEFORE INSERT ON `raut_flat_temperature_control_controller` FOR EACH ROW
BEGIN
INSERT INTO `reeve`.`raut_controller` (
`raut_controller_type_id`
)
VALUES (
(SELECT `id` FROM `reeve`.`raut_controller_type` WHERE table_name = 'raut_flat_temperature_control_controller')
);
SET NEW.id = LAST_INSERT_ID();
END