我在.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:{FrozenTableName: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是因为UserhasMany具有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);
  });

10-02 03:25