Navicat使用踩坑之触发器(trigger)

266 阅读1分钟

| 数据库为mysql8数据库;

有数据库定义结构如下:

CREATE TABLE `authorities` (
  `username` varchar(255)  NOT NULL COMMENT '用户名',
  `authority` varchar(255) NOT NULL COMMENT 'token',
  `start_time` datetime DEFAULT NULL COMMENT '秘钥开始时间',
  `end_time` datetime DEFAULT NULL COMMENT '秘钥结束时间',
  KEY `fk_authorities_users` (`username`),
  CONSTRAINT `fk_authorities_users` FOREIGN KEY (`username`) REFERENCES `user` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

定义触发器如下:

CREATE TRIGGER `insert_start_time`
    BEFORE INSERT
    ON `authorities`
    FOR EACH ROW
BEGIN
    SET NEW.start_time = NOW();
    SET NEW.end_time = DATE_ADD(NEW.start_time, INTERVAL 7 DAY);
END;

CREATE TRIGGER `update_end_time`
    BEFORE UPDATE
    ON `authorities`
    FOR EACH ROW
BEGIN
    SET NEW.start_time = NOW();
    if NEW.end_time is null THEN
        SET NEW.end_time = DATE_ADD(NOW(), INTERVAL 7 DAY);
    end if;
END;

出现问题如下:

在Navicat中直接修改数据后,提交;触发器并未触发;

检查日志发现触发器并未触发;

多处排查并未发现异常;

image.png

N长时间之后,尝试使用其他数据库连接工具; 执行语句后触发器更新;

INSERT INTO `nethub`.`authorities` (`username`, `authority`)
VALUES ('nethub_b8d6d8abbcd2_0', 'asdw');