首页 > 其他分享 >ERROR 1067 (42000): Invalid default value for 'xxx字段'

ERROR 1067 (42000): Invalid default value for 'xxx字段'

时间:2023-01-10 22:33:06浏览次数:47  
标签:bin default utf8 xxx 1067 DEFAULT COLLATE timestamp NULL

报错版本:mysql-5.7.35

1、报错完整提示信息;

ERROR 1067 (42000): Invalid default value for 'LOCK_TIME_'

2、原因:

使用source命令迁移activiti表数据sql的时候,其中act_evt_log表create报错,完整的create语句:

CREATE TABLE `ACT_EVT_LOG`  (
  `LOG_NR_` bigint(20) NOT NULL AUTO_INCREMENT,
  `TYPE_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `PROC_DEF_ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `PROC_INST_ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `EXECUTION_ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `TASK_ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `TIME_STAMP_` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  `USER_ID_` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `DATA_` longblob,
  `LOCK_OWNER_` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `LOCK_TIME_` timestamp(3) DEFAULT NULL,
  `IS_PROCESSED_` tinyint(4) DEFAULT 0,
  PRIMARY KEY (`LOG_NR_`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

可以看到报错的字段LOCK_TIME_为timestamp类型,且默认为null,因为是mysql5.7.x版本,会有一个严格模式,而在此模式下默认是不允许设置日期时间的值为全0值的。

3、解决:

调整sql_mode、explicit_defaults_for_timestamp两个参数的值。(下面步骤是临时调整,mysql重启会失效,如果需要永久调整的话得将配置写在my.cnf中,然后重启mysql让其生效)
步骤如下:

# 1.查询sql_mode的值
mysql> select @@sql_mode;

# 2.将查询的值去掉NO_ZERO_IN_DATE,NO_ZERO_DATE,将剩余的值重新赋给sql_mode
mysql> set global sql_mode = '修改后的值';

# 3.同时MySQL5.7.x的explicit_defaults_for_timestamp默认值为OFF,这与sql_mode的NO_ZERO_DATE也是相冲突的,需要修改为ON。
mysql> show variables like 'explicit_defaults_for_timestamp';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF   |
+---------------------------------+-------+
1 row in set (0.00 sec)
mysql> set @@explicit_defaults_for_timestamp = 1;
Query OK, 0 rows affected (0.00 sec)

标签:bin,default,utf8,xxx,1067,DEFAULT,COLLATE,timestamp,NULL
From: https://www.cnblogs.com/liulangdian/p/17041564.html

相关文章