一个疏忽引起的bug

      来看建表语句:

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> 

         呵呵哒。

         要吸取教训,该测试的要测试,该监控的要监控。

         不多说。      

发布了2229 篇原创文章 · 获赞 4764 · 访问量 2014万+

猜你喜欢

转载自blog.csdn.net/stpeace/article/details/105456273