Node.js 为什么Sequelize会将所有事物的名称复数化/单数化?如何完全阻止这种情况?

Node.js 为什么Sequelize会将所有事物的名称复数化/单数化?如何完全阻止这种情况?,node.js,sequelize.js,Node.js,Sequelize.js,.create()方法函数和模型定义中我的表的名称从单数到复数,反之亦然为什么Sequelize有这个功能?为什么禁用它会如此不稳定 我的数据库中的表名是(如代码中的)“用户”、“电子邮件”、“设置”。但是在执行INSERT和SELECT SQL语句时,Sequelize会将名称单数化,就好像我需要一个库来为我的数据库表选择最佳名称一样!因此,一些插入失败 这是我的密码: // DEPENDENCIES const Sequelize = require('sequelize'); //

.create()方法函数和模型定义中我的表的名称从单数到复数,反之亦然为什么Sequelize有这个功能?为什么禁用它会如此不稳定

我的数据库中的表名是(如代码中的)“用户”、“电子邮件”、“设置”。但是在执行INSERT和SELECT SQL语句时,Sequelize会将名称单数化,就好像我需要一个库来为我的数据库表选择最佳名称一样!因此,一些插入失败

这是我的密码:

// 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语句仍然有一个类似的问题:它们被奇点化了


这可能是一个bug吗?

它与您定义的关联有关。对于设置,我们有
hasOne
关系。因此这个名字是单数的。对于电子邮件,我们的
有许多
,并且有复数形式

让我们看下面的一个例子

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

谢谢你的解释!我理解。但这一功能到底有什么用处呢?我怎样才能摆脱它呢?因为它,我不能在“设置”表中插入条目,因为Sequelize使其成为单数。我不确定您不能插入是什么意思。插入时,您可以使用单数名称。这很有效!谢谢:)不过我还是觉得这个功能需要删除。很高兴它能帮上忙。