gpt4 book ai didi

node.js - 为什么 Sequelize 会对所有事物的名称进行复数/单数化?以及如何完全阻止这种情况?

转载 作者:行者123 更新时间:2023-12-02 16:57:01 24 4
gpt4 key购买 nike

.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);
});

我使用了电子邮件,因为用户电子邮件hasMany关系。如果我将关系类型更改为 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);
});

关于node.js - 为什么 Sequelize 会对所有事物的名称进行复数/单数化?以及如何完全阻止这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com