亚马逊的云平台提供数据库服务RDShtml
把数据库从本地VM迁移到RDS以后,测试时出现一个问题mysql
createDateGMT cannot be nullsql
createDateGMT 是一个timestamp类型,当数据进行插入操做时能够自动生成为当前时间。当Entity里面对应的attribute没有设值,default为null。数据库
在以前的VM测试时当插入的值为null, mysql会自动设为当前时间。可是在RDS上面却报了这样的错误。jsp
通过简单的检查对比:测试
RDS上用的是5.6.12版本,而以前的VM大部分用了5.5.27版本,只有一个用了5.6.12版本google
RDS上面的时区是UTCspa
针对上面的差别测试了VM上面5.6.12版本,没有问题。code
也测试了在RDS上面更改时区, 问题继续server
看上去跟时区和版本没有关系?
因而定位问题是RDS上面的配置的问题,google了RDS相关的问题发现一下帖子
http://stackoverflow.com/questions/18264942/how-to-import-mysql-binlog-that-contains-inserts-of-a-timestamp-field-with-defau
https://forums.aws.amazon.com/thread.jspa?threadID=132676#
恍然大悟,终于仍是跟mysql的版本和设置有关系。
mysql的timestamp 类型有一些特性,好比
The first TIMESTAMP
column in a table, if not declared with the NULL
attribute or an explicit DEFAULT
or ON UPDATE
clause, is automatically assigned the DEFAULT CURRENT_TIMESTAMP
and ON UPDATE CURRENT_TIMESTAMP
attributes.
mysql 5.6.6 以后引入了一个参数-explicit_defaults_for_timestamp
http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
这个参数的目的就是提供一个选择去turn off timestamp类型的特性,好比
No TIMESTAMP
column is assigned the DEFAULT CURRENT_TIMESTAMP
or ON UPDATE CURRENT_TIMESTAMP
attributes automatically. Those attributes must be explicitly specified.
而RDS上的mysql上的explicit_defaults_for_timestamp值为1,说明已经turn off了timestamp的特性. 而createDateGMT又没有设置ON UPDATE等属性,因此不会自动生成当前时间。
解决办法有几种
1. 降级MySQL版本,等待amazon解决
2. 用trigger生成时间
3. 在程序里去判断和设置时间