如何使Sequelize使用奇异的表名

我有一个名为User的模型,但每当我试图保存在数据库中时,Sequelize都会查找表USERS。 有谁知道如何设置Sequelize使用奇异的表名? 谢谢。

该文档声明,您可以使用属性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' }) 

虽然接受的答案是正确的,但是您可以对所有表格执行一次,而不必为每个表格单独执行。 您只需将类似的选项对象传入Sequelize构造函数,如下所示:

 var Sequelize = require('sequelize'); //database wide options var opts = { define: { //prevent sequelize from pluralizing table names freezeTableName: true } } var sequelize = new Sequelize('mysql://root:123abc@localhost:3306/mydatabase', opts) 

现在当你定义你的实体时,你不必指定freezeTableName: true

 var Project = sequelize.define('Project', { title: Sequelize.STRING, description: Sequelize.TEXT })