亞馬遜的雲平臺提供數據庫服務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. 在程序裏去判斷和設置時間