MySQL 使用timestamp的坑

在创建表的时候 created_at 跟 updated_at 字段都使用了 timestamp 字段来进行存储。
数据库版本是 5.7.26
一开始的语句是这样的

1
2
3
4
5
create table `test` (
#...
created_at timestamp,
updated_at timestamp
)

但是报错

1067 - Invalid default value for ‘updated_at’

修改为

1
2
3
4
5
create table `test` (
#...
created_at timestamp null,
updated_at timestamp null
)

解决问题

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~