thanks in advance for your hepl.
I have two models Menu and Window with their associations
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.
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
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
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.