我有一张桌子
CREATE TABLE `openings` (
`code` varchar(9) COLLATE utf8mb4_unicode_ci NOT NULL,
`validFrom` date NOT NULL,
`validTo` date NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
以及以下BEFORE INSERT
触发器在DATE
列上设置默认值:
CREATE TRIGGER `openings_default_validFrom` BEFORE INSERT ON `openings` FOR EACH ROW
IF ( ISNULL(new.validFrom) ) THEN
SET new.validFrom=CURDATE();
END IF;
最后,填充表的过程:
CREATE PROCEDURE sp_populate_openings (IN prefix CHAR(4), IN num INT)
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < num DO
INSERT INTO openings (code,validTo) VALUES (prefix, '2017-06-31');
SET i = i + 1;
END WHILE;
END;
当我在MySQL的本地(Windows)安装版本5.7.9-log中运行该过程时,所有这一切都正常,但是当我尝试在服务器(Linux)版本5.6.33中运行它时,触发器似乎不起作用,因为从liquibase调用过程时出现以下错误:
Caused by: java.sql.SQLException: Field 'validFrom' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
我可以手动插入一行而不会出现任何错误,但值设置为0000-00-00。
知道为什么失败或如何解决它?在升级服务器版本之前,我更愿意尝试其他选项。