[英]Parse model and association hasMany from a raw query
thanks in advance for your hepl.提前感谢您的帮助。
I have two models Menu and Window with their associations我有两个模型 Menu 和 Window 及其关联
db.menus.hasMany(db.windows);
db.windows.belongsTo(db.windows);
I'm trying to make a plane query with a output similar to this:我正在尝试使用与此类似的输出进行平面查询:
'menu':
{'description':'Menu 1',
"windows": [
{"description": "windows 1"},
{"description": "windows 2"},
{"description": "windows 3"},
]
}
When i put the assosiation hasOne instead of hasMany at least the windows is inside of the menu, but with a row for each windows, but with the hasMany the information is inclued as part of the menu and I want a object of the windows.当我将关联 hasOne 而不是 hasMany 时,至少窗口在菜单内,但每个窗口都有一行,但是 hasMany 信息作为菜单的一部分包含在内,我想要一个窗口对象。
const options = {
model: db.menus,
// mapToModel: true,
// hasJoin: true,
include: [{
model: db.ventanas
}]
};
db.menus._validateIncludedElements(options);
db.sequelize.query(Query, options)
.then(MenuWindows => {
console.log(MenuWindows);
});
I have proved a lot of convinations but no one works properly and I wondering if there is something that I havent done, as I said when I put the assosiation hasOne the option hasJoin works otherwise that give me this error Unhandled rejection TypeError: Cannot read property 'model' of undefined我已经证明了很多说服,但没有人能正常工作,我想知道是否有我没有做过的事情,正如我在放置关联 hasOne时所说的那样,选项hasJoin否则会给我这个错误未处理的拒绝类型错误:无法读取属性未定义的“模型”
BTW, with the findAll the sequelize works fine, but I'm making a complex query with more tables but just showing the menus and windows, so I infer that the association is fine and the problem is with the way I'm mapping the output顺便说一句,使用 findAll,sequelize 工作正常,但我正在使用更多表进行复杂查询,但只显示菜单和窗口,所以我推断关联很好,问题在于我映射输出的方式
After a thousand of attempts it worked just putting the alias of the table in the model经过一千次尝试,它只是将表的别名放入模型中
model: db.menus,
as: 'menu',
hasJoin: true,
include: [{
as: 'windows',
model: db.windows
}]
};
db.menus._validateIncludedElements(options);
db.sequelize.query(query, options)
.then(menuWindows => {
console.log(menuWindows);
}); ```
have you tried this...你有没有试过这个...
return sequelize.query(/* query */, {
nest: true,
type: sequelize.QueryTypes.SELECT
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.