來看建表語句:
CREATE TABLE `xxx` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`clear_time` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
這三個字段對應的代碼賦值爲:
obj.Id = 0 // 實際插入代碼時候,id會自增
UserId = 1
ClearTime = currentTime
現在進行一個優化,需要讓clear_time爲1970-01-01 00:00:00, 於是在代碼中直接去掉了上面的第三行代碼,如下:
obj.Id = 0 // 實際插入代碼時候,id會自增
UserId = 1
// ClearTime = currentTime
於是,根據數據庫建表語句中的default值,想當然地以爲clear_time是1970-01-01 00:00:00, 以爲這麼簡單的一個修改不會出錯,但是,結果呵呵噠了。 其實,代碼框架在處理時,把clear_time當成了空串,所以是按照第三個insert語句來執行的(而不是第一個):
mysql> insert into xxx set user_id = 1;
Query OK, 1 row affected (0.00 sec)
mysql> insert into xxx set user_id = 1 and clear_time = '1970-01-01 00:00:00';
Query OK, 1 row affected (0.00 sec)
mysql> insert into xxx set user_id = 1 and clear_time = ''; // 就是這裏呵呵噠了
ERROR 1292 (22007): Incorrect datetime value: '' for column 'clear_time' at row 1
mysql>
mysql>
mysql>
mysql> select * from xxx;
+----+---------+---------------------+
| id | user_id | clear_time |
+----+---------+---------------------+
| 1 | 1 | 1970-01-01 00:00:00 |
| 2 | 1 | 1970-01-01 00:00:00 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
mysql>
呵呵噠。
要吸取教訓,該測試的要測試,該監控的要監控。
不多說。