【问题标题】:Sequelize-cli how not to include timestamps while generating model and migration files?Sequelize-cli 如何在生成模型和迁移文件时不包含时间戳?
【发布时间】:2018-07-14 01:58:01
【问题描述】:

只需按照 sequelize doc (http://docs.sequelizejs.com/manual/tutorial/migrations.html),我们可以使用 sequelize-cli 命令生成模型和迁移文件,如下所示:

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

但是,在迁移文件中,可以找到两个额外的时间戳值,它们将被添加到数据库中:

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

我知道我可以手动设置timestamp: false 和/或删除这两个条目,但最好在生成模型/迁移文件时设置一个选项以不具有这些时间戳。有没有这样的方法?

【问题讨论】:

    标签: node.js database-migration sequelize-cli


    【解决方案1】:

    您也可以在config.json 中设置选项。它适用于所有型号。

    "dialect": "mysql",
    "logging": false,
    "define": {
       "timestamps": true
    }
    

    【讨论】:

    • 不幸的是,这不起作用。迁移仍会将 createdAt 和 updatedAt 字段添加到数据库中,但不会出现在模型上。您仍然需要进入迁移文件并手动删除这两个字段。
    【解决方案2】:

    对 sequelize-cli 使用 --underscored 参数。

    例子:

    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
    

    或使用包含以下内容的文件“config.json”:

    {
      "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"
      }
    }
    

    ..并将其作为参数传递给命令行:

    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
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2015-03-06
      • 2020-05-29
      • 2020-02-01
      • 2021-03-17
      • 2020-05-25
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      相关资源
      最近更新 更多