Parse model and association hasMany from a raw query

Adreso :

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

Greg Belyea :

have you tried this...

return sequelize.query(/* query */, {
    nest: true,
    type: sequelize.QueryTypes.SELECT
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24966&siteId=1
Recommended