sequelize.js - Sequelize 别名关联上的急切加载错误
问题描述
嗨,我有以下 index.js
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var CONFIG = require('../config/config');
var sequelize = new Sequelize(CONFIG.database, CONFIG.user, CONFIG.password, {
host: CONFIG.host,
dialect: CONFIG.dialect,
port: CONFIG.port,
operatorsAliases: Sequelize.Op,
logging: false
});
var db = {};
fs.readdirSync(__dirname)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function (file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function (modelName) {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
而且我想包含两次具有不同条件的模型,因此我计划在这种情况下使用别名:
module.exports = function (sequelize, DataTypes) {
var TSection = sequelize.define('TSection', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(500),
allowNull: true
},
TestId: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
field: 'test_id',
references: {
model: 'test',
key: 'id'
}
},
},
{
tableName: 't_sections'
});
TSection.associate = function (models) {
TSection.belongsTo(models.Test), {
foreignKey: 'id',
as: 'sections'
},
TSection.hasMany(models.TQuestion), {
foreignKey: 'id'
}
};
return TSection;
};
和
module.exports = function (sequelize, DataTypes) {
var Test = sequelize.define('Test', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(500),
allowNull: true,
},
},
{
tableName: 'tests'
})
Test.associate = function (models) {
Test.hasMany(models.TSection), {
foreignKey: 'id'
}
}
return Test;
};
当我尝试获取以下信息时:
const test = await Test.findOne({
attributes: ['id', 'name'],
include: [{
model: TSection,
attributes: ['id', 'name'],
}, {
model: TSection,
as: 'sections'
}]
});
我有这个错误,我不知道是什么原因造成的:注意:我尝试了几种组合,包括和不包括上述模型
UnhandledPromiseRejectionWarning: Error: Error: SequelizeEagerLoadingError: TSection is associated to Test using an alias. You've included an alias (sections), but it does not match the alias(es) defined in your association (TSections).
我想你想知道我的目标是计算总部分的数量,但只检索一个(与给定条件匹配的那个)
谢谢!
解决方案
您在关联中添加了额外的括号。它们应该如下所示:
TSection.associate = function (models) {
TSection.belongsTo(models.Test, {
foreignKey: 'id',
as: 'sections'
});
TSection.hasMany(models.TQuestion, {
foreignKey: 'id'
});
};
和
Test.associate = function (models) {
Test.hasMany(models.TSection, {
foreignKey: 'id'
})
}
推荐阅读
- c# - 连接蓝牙打印机,打印一些东西
- laravel - Laravel,访客和用户的公共页面,会话表 user_id null
- azure - 使用来自 Web 套接字安全链接的数据工厂复制数据
- php - 如何在 html 表中使用 imap 显示所有 gmail 电子邮件?
- javascript - 对数范围输入滑块 - 增加阈值的步长
- reactjs - 模拟点击并获取状态返回值
- python - 如何从 python 中的 tarfile 将文件提取到不同的目标文件名?
- bluetooth-lowenergy - 蓝牙mesh - 树莓派
- laravel - 如何在 Spatie 中转换所有媒体?
- javascript - PrismJS 中的自定义 Javascript 对象