Repository navigation
Closed
Description
Given three models:
const UserAccount = mysql.define('UserAccount', modelAttrs, modelOpts);
const Roles = mysql.define('Roles', modelAttrs, modelOpts);
const RoleUser = mysql.define('RoleUser', modelAttrs, modelOpts);RoleUser represents a table:
createdAt updatedAt RoleId UserAccountId
------------------- ------------------- ------ ---------------
2016-09-04 04:50:09 2016-09-04 04:50:13 1 181
2016-07-21 18:05:29 2016-07-21 18:05:29 2 181
Their association. Only Roles and UserAccount are used.
Roles.belongsToMany(UserAccount, {
through: RoleUser,
});
UserAccount.belongsToMany(Roles, {
through: RoleUser,
});I want to retrieve a UserAccount with all its Roles:
UserAccount.findOne({
where: {
email: '[email protected]'
},
attributes: ['id', 'fname', 'lname', 'email'],
include: [{
model: Roles,
attributes: ['role_name', 'id'],
required: true, // don't know what for this option is, it doesn't make SQL string any better
}],
})
.then(users => {
console.log(users);
})And users response is as follows. Please pay attention to Roles.RoleUser includes. Why does it retrieve RoleUser data? I didn't give a command for that. It is redundant here. Roles would be enough. How to get rid of them, what option to provide findOne in order to exclude them from result?
{ id: 181,
fname: 'John',
lname: 'Doe',
email: '[email protected]',
Roles:
[ { role_name: 'admin',
id: 1,
RoleUser:
{ RoleId: 1,
UserAccountId: 181,
createdAt: Sun Sep 04 2016 12:50:09 GMT+0800 (Malay Peninsula Standard Time),
updatedAt: Sun Sep 04 2016 12:50:13 GMT+0800 (Malay Peninsula Standard Time) } },
{ role_name: 'member',
id: 2,
RoleUser:
{ RoleId: 2,
UserAccountId: 181,
createdAt: Fri Jul 22 2016 02:05:29 GMT+0800 (Malay Peninsula Standard Time),
updatedAt: Fri Jul 22 2016 02:05:29 GMT+0800 (Malay Peninsula Standard Time) } } ] } And an SQL string generated by Sequelize for this operation.
SELECT
`UserAccount`.*,
`Roles`.`role_name` AS `Roles.role_name`,
`Roles`.`id` AS `Roles.id`,
`Roles.RoleUser`.`RoleId` AS `Roles.RoleUser.RoleId`,
`Roles.RoleUser`.`UserAccountId` AS `Roles.RoleUser.UserAccountId`,
`Roles.RoleUser`.`createdAt` AS `Roles.RoleUser.createdAt`,
`Roles.RoleUser`.`updatedAt` AS `Roles.RoleUser.updatedAt`
FROM
(SELECT
`UserAccount`.`id`,
`UserAccount`.`fname`,
`UserAccount`.`lname`,
`UserAccount`.`email`
FROM
`UserAccount` AS `UserAccount`
WHERE `UserAccount`.`email` = '[email protected]'
LIMIT 1) AS `UserAccount`
LEFT OUTER JOIN (
`RoleUser` AS `Roles.RoleUser`
INNER JOIN `Roles` AS `Roles`
ON `Roles`.`id` = `Roles.RoleUser`.`RoleId`
)
ON `UserAccount`.`id` = `Roles.RoleUser`.`UserAccountId` ;Reacted by Felix Becker, Can Ho, Roni Bar-David, Yehonatan Tsirolnik and Efrony
Activity
felixfbecker commentedon Sep 4, 2016
Maybe
through: falseorthrough: {attributes: []}wzup commentedon Sep 4, 2016
Yes,
through: {attributes: []}did the trick. But it should be explained somewhere in docs.Query config:
felixfbecker commentedon Sep 4, 2016
See http://docs.sequelizejs.com/en/v3/api/model/#findall and look for
options.include[].attributes. It is documented, just not the behaviour that it completely removes the through object. I didn't even know that myself 😉papb commentedon Jan 8, 2020
I think our manuals should mention this more clearly, I will reopen this as a docs issue.
And I'm already working on it :)