.create() 方法函数和模型定义中的表名称从单数变为复数,反之亦然。 为什么 Sequelize 具有此功能? 为什么禁用它会如此不稳定?
我的数据库中的表名称是(如代码中所示)“user”、“email”、“settings”。但是,当执行 INSERT 和 SELECT SQL 语句时,Sequelize 会将名称单一化,就好像我需要一个库为我的数据库表选择最佳名称一样!因此,某些 INSERT 会失败。
这是我的代码:
// DEPENDENCIES
const Sequelize = require('sequelize');
// Connection set up:
const sequelize = new Sequelize(
'sql_database_1',
'sqlusername1',
'dbpassw0rd',
{ // Sequelize options:
host: 'localhost',
port: 3306,
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
logging: console.log,
define: {
freezeTableName: true, // Do not change my table names.
timestamps: false // I will do this individually, thanks.
},
});
// Set up models:
const User = sequelize.define('user',
{ // Database columns:
user_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
column1: Sequelize.STRING,
});
const Settings = sequelize.define('settings',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: Sequelize.INTEGER,
column1: Sequelize.STRING
});
const Email = sequelize.define('email',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: Sequelize.INTEGER,
column1: Sequelize.STRING
});
// Set up associations:
User.hasOne(Settings,
{ // Options:
foreignKey: 'owner_id'
});
User.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
// Actions:
sequelize
.sync({
force: true
})
.then(function() {
User
.create({
column1: 'test123',
settings: { // STACK OVERFLOW: Not working because of Sequelize singularizing the name.
column1: 'This is dummy address'
},
emails: [ // STACK OVERFLOW: I need to write this table name in plural to work because Sequelize is changing MY names...
{ column1: 'Some data here' },
{ column1: 'Other data there' }
],
},
{
include: [Settings, Email]
})
})
.then(function() {
User
.findOne({
include: [Settings, Email],
})
.then(function(result) {
console.log('FindAll results:\n', JSON.stringify(result));
});
});
如您所见,我在专用于设置 Sequelize 选项的对象中使用“define: { freezeTableName: true }”。它仅在创建新表名称时起作用:它不会使它们复数。 INSERT 和 SELECT 语句仍然存在类似的问题:它们被单一化。
这会是一个错误吗?
这与您定义的关联有关。对于设置,我们有
hasOne
关系;因此,这个名字是单数的。对于电子邮件,我们有 hasMany
关系;因此,这个名字是复数的。
让我们看下面的例子。
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
const Email = sequelize.define('emails', {
text: Sequelize.STRING,
});
User.hasMany(Email);
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test1234',
emails: {
text: 'this is dummy Email123'
},
}, { include: [Email] }))
.then(user => {
console.log(user.dataValues);
});
我使用了
emails
,因为User
与hasMany
有Email
关系。
如果我将关系类型更改为 hasOne
,我将使用单数名称。
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
const Email = sequelize.define('emails', {
text: Sequelize.STRING,
});
User.hasOne(Email);
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test1234',
email: {
text: 'this is dummy Email123'
},
}, { include: [Email] }))
.then(user => {
console.log(user.dataValues);
});