sequelize.js TIMESTAMP not DATETIME
You can also use the moment for creating a timestamp:
const moment = require('moment-timezone');
createdAt: {
type: DataTypes.NOW,
allowNull: false,
defaultValue: moment.utc().format('YYYY-MM-DD HH:mm:ss'),
field: 'createdAt'
},
According to the Sequelize Documentation, you can set a defaultValue of Sequelize.NOW to create a timestamp field. This has the effect but relies on Sequelize to actually populate the timestamp. It does not create a "CURRENT_TIMESTAMP' attribute on the table.
var Foo = sequelize.define('Foo', {
// default values for dates => current time
myDate: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
});
So, this does accomplish the end goal of having a timestamp field, but it is controlled through Sequelize and not through the actual database engine.
It also appears to work on databases that do not have a timestamp functionality, so that may be a benefit.
Reference URL: http://sequelize.readthedocs.org/en/latest/docs/models-definition/#definition
Just pass in 'TIMESTAMP' string to your type
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.createTable('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
created_at: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updated_at: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
}
});
}
};