본문 바로가기
  • AI (Artificial Intelligence)
Programming/JavaScript, Node.js

[sequelize] DATETIME to TIMESTAMP

by 로샤스 2021. 7. 6.

Ref. https://stackoverflow.com/questions/29652538/sequelize-js-timestamp-not-datetime

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
      }
    });
  }
};

 

OR:

module.exports = (sequelize, type) => {
    return sequelize.define('blog', {
        blogId: {
          type: type.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        text: type.STRING,
        createdAt:{
            type: 'TIMESTAMP',
            defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
            allowNull: false
        },
        updatedAt:{
            type: 'TIMESTAMP',
            defaultValue: sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
            allowNull: false
        }
    })
}

댓글