https://github.com/sequelize/sequelize/issues/4896

1
2
3
4
5
6
7
8
9
10
11
12
13
const { Sequelize, DataTypes } = require('sequelize')

const User = sequelize.define('user', {
// ...
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
}
})
1
2
3
4
5
6
7
8
9
10
11
mysql> desc users;
+-----------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | UNI | NULL | |
| password | varchar(255) | NO | | NULL | |
| avatar | varchar(255) | YES | | NULL | |
| createdAt | datetime | YES | | CURRENT_TIMESTAMP | |
| updatedAt | datetime | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+--------------+------+-----+-------------------+-----------------------------+