如何使 Sequelize 使用单数表名

2022-01-19 00:00:00 sql node.js mysql sequelize.js

我有一个名为 User 的模型,但 Sequelize 会在我尝试保存在数据库中时查找表 USERS.有谁知道如何将 Sequelize 设置为使用单数表名?谢谢.

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

推荐答案

docs 声明您可以使用属性 freezeTableName.

The docs state that you can use the property freezeTableName.

请看一下这个例子:

var Bar = sequelize.define('Bar', { /* bla */ }, {
  // don't add the timestamp attributes (updatedAt, createdAt)
  timestamps: false,

  // don't delete database entries but set the newly added attribute deletedAt
  // to the current date (when deletion was done). paranoid will only work if
  // timestamps are enabled
  paranoid: true,

  // don't use camelcase for automatically added attributes but underscore style
  // so updatedAt will be updated_at
  underscored: true,

  // disable the modification of tablenames; By default, sequelize will automatically
  // transform all passed model names (first parameter of define) into plural.
  // if you don't want that, set the following
  freezeTableName: true,

  // define the table's name
  tableName: 'my_very_custom_table_name'
})

相关文章