Even if i use FreezableTableName:True it stores table in DB with Plural one.
//user.js
-------------------
'use strict';
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define('user', {
name: DataTypes.STRING,
email: DataTypes.TEXT
}, {});
user.associate = function(models) {
// associations can be defined here
};
return user;
};
//migration file xxxxxxx-create-user.js
----------------------------------
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.TEXT
},
password: {
type: Sequelize.TEXT
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
}
};
它在数据库中添加了复数表名,如何避免这种情况?
答案 0 :(得分:0)
首先,实际上创建表的一个是迁移代码。
module.exports = {
up: (queryInterface, Sequelize) => {
// you're specifying `users` instead of `user` here
return queryInterface.createTable('users', {
因此,请先用users
替换user
。
为使序列化模型知道在搜索表user
时要在哪里查找,您还必须完成一个额外的步骤。
const user = sequelize.define('user', {
name: DataTypes.STRING,
email: DataTypes.TEXT
}, { tableName: 'user' }); // pass table name option here
现在,您将可以再次进行查询。