背景
线上日志报错:
对应的建表语句:
分析
问题总结:
测试环境和生产环境 inserttime 字段的定义相同:inserttime timestamp default CURRENT_TIMESTAMP not null comment '插入时间'
,差别之处在于版本号。
测试环境:
生产环境:
带着关键字timestamp default CURRENT_TIMESTAMP
和Column 'inserttime' cannot be null
搜索Google。找到MySQL案例一则。
测试环境:
生产环境:
建表语句的意思是insert数据时,如果 inserttime 数据为空,则用系统default时间,这也是测试环境没有发现这个问题的原因。
生产环境里,explicit_defaults_for_timestamp=ON
,即如果 inserttime 数据为空,则报错Column 'inserttime' cannot be null
。注意:可以直接通过客户端如 dataGrip insert 一条 inserttime is null 的数据。
解决方法
- 修改生产参数配置,一来不建议,二来没有修改权限,需要联系DBA执行(还说服他们):
set explicit_defaults_for_timestamp=OFF
- mybatis使用insertSelective而不用insert,建议:
- 修改DB设置,不建议:
alter table var_monitor modify column inserttime timestamp default CURRENT_TIMESTAMP null comment '插入时间';
参考