4

Just following the sequelize doc ( http://docs.sequelizejs.com/manual/tutorial/migrations.html ), we can generate a model and a migration file using the sequelize-cli command like:

sequelize model:generate --name Users --attributes firstName:string,lastName:string,bio:text

but then, in the migration file, one can find two additional timestamps values that will be added to the DB:

  createdAt: {
    allowNull: false,
    type: Sequelize.DATE
  },
  updatedAt: {
    allowNull: false,
    type: Sequelize.DATE
  }

I know I can set timestamp: false and/or deleting these two entries manually, but it would be better to set an option while generating the model/migration files not to have these timestamps. Is there such a way?

swiss_knight
  • 5,787
  • 8
  • 50
  • 92

2 Answers2

3

You can set the options in config.json as well. It works for all models.

"dialect": "mysql",
"logging": false,
"define": {
   "timestamps": true
}
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
  • 8
    Unfortunately this does not work. The migration will still add the createdAt and updatedAt fields to the database, but will not be on the model. You still have to go into the migration file and remove the two fields manually. – Xipooo Jan 25 '19 at 16:46
3

Use --underscored param for sequelize-cli.

Example:

sequelize-cli model:generate --name User --attributes login:string,password:string,token:string,token_exp:date,firstName:string,lastName:string,email:string,phone:string --underscored

or use file 'config.json' with this contents:

{
  "define": {
    "underscored": true,
    "freezeTableName": true,
    "charset": "utf8",
    "dialectOptions": {
      "collate": "utf8_general_ci"
    },
    "timestamps": true,
    "createdAt": "created_at",
    "updatedAt": "updated_at",
    "createdBy": "created_by",
    "updatedBy": "updated_by"
  }
}

..and pass it to command line as param:

sequelize-cli model:generate --name User --attributes login:string,password:string,token:string,token_exp:date,firstName:string,lastName:string,email:string,phone:string --config config.json