我遇到了一个奇怪的错误,我收到一条关于我的数据库表的复数版本的消息,它被分组到我的后续查询中。我扫描了我的模型和路线,但找不到我的表名复数形式的任何实例,当我检查数据库表时,结构没有提到任何复数形式。当我记录查询时,查询的这一部分似乎出现了复数错误

images AS images LEFT OUTER JOIN descriptions AS description ON

这是完整的错误消息:

Unhandled rejection SequelizeDatabaseError: ER_NO_SUCH_TABLE: Table 'assistant.descriptions' doesn't exist

这是我的 Images 模型:

module.exports = function(sequelize, DataTypes){
var Images = sequelize.define('images', {
    pattern: DataTypes.STRING,
    color: DataTypes.STRING,
    imageUrl: DataTypes.STRING,
    imageSource: DataTypes.STRING,
    description_id: DataTypes.INTEGER
}, {
    classMethods: {
        associate: function(db) {
            Images.belongsTo(db.Description, {foreignKey: 'description_id'});
        }
    }
});
    return Images;
}

这是我的 Description 模型:
module.exports = function(sequelize, DataTypes) {
var Description = sequelize.define('description', {
    description_id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    color: DataTypes.STRING,
    body: DataTypes.STRING
});
    return Description;
}

以下是我如何在 dbIndex 中关联模型:
var Sequelize      = require('sequelize');
var sequelize = new Sequelize("assistant", "admin", "pwd", {
    host: "host",
    port: 3306,
    dialect: 'mysql'
});
var db = {};

db.Description = sequelize.import(__dirname + "/descriptionModel");

db.Images = sequelize.import(__dirname + "/imagesModel");

db.Images.associate(db);

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

这是我的查询:
router.get('/:pattern/:color/result', function(req, res, image){

    console.log(req.params.color);
    console.log(req.params.pattern);

    db.Images.findAll({
        where: {
            pattern: req.params.pattern,
            color: req.params.color
        },
        include: [db.Description],
        attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id']
    }).then(function(image){
        //console.log(doc.descriptions_id);
        res.render('pages/suit-result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
            })
        });
});

这是错误前的查询:
Executing (default): SELECT `images`.`id`, `images`.`pattern`, `images`.`color`, `images`.`imageUrl`, `images`.`imageSource`, `images`.`description_id`, `description`.`description_id` AS `description.description_id`, `description`.`color` AS `description.color`, `description`.`body` AS `description.body`, `description`.`createdAt` AS `description.createdAt`, `description`.`updatedAt` AS `description.updatedAt` FROM `images` AS `images` LEFT OUTER JOIN `descriptions` AS `description` ON `images`.`description_id` = `description`.`description_id` WHERE `images`.`pattern` = 'solid' AND `images`.`color` = 'navy-blue';

最佳答案

默认情况下,Sequeulize 会将您的表名复数化。要关闭它,请将选项 freezeTableName: true 包含到您不希望复数形式的所有模型定义选项中。例如,使用描述模型:

var Description = sequelize.define('description', {
  description_id: {
    type: DataTypes.INTEGER,
    primaryKey: true
  },
  color: DataTypes.STRING,
  body: DataTypes.STRING
}, {
  freezeTableName: true
});

关于mysql - Sequelize - 表复数化错误,表中没有实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33318862/

10-13 00:59