Skip to content

Eager loading retrieves data from "through" table. How to disable? #6541

Closed

Description

@wzup

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` ;

Activity

  1. felixfbecker commented on Sep 4, 2016

    @felixfbecker
    Contributor

    Maybe through: false or through: {attributes: []}

  2. added
    type: feature
    on Sep 4, 2016
  3. wzup commented on Sep 4, 2016

    @wzup
    Author

    Yes, through: {attributes: []} did the trick. But it should be explained somewhere in docs.

     { id: 181,
      fname: 'John',
      lname: 'Doe',
      email: '[email protected]',
      Roles: 
       [ { role_name: 'admin', id: 1 },
         { role_name: 'member', id: 2 } ] } 
    

    Query config:

    UserAccount.findOne({
        where: {
            email: '[email protected]'
        },
        attributes: ['id', 'fname', 'lname', 'email'],
        include: [{
            model: Roles,
            attributes: ['role_name', 'id'],
            through: {attributes: []}, //  <== Here
            required: true,
        }],
    })
    
  4. felixfbecker commented on Sep 4, 2016

    @felixfbecker
    Contributor

    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 😉

  5. added and removed
    type: feature
    on Sep 4, 2016
  6. papb commented on Jan 8, 2020

    @papb
    Member

    I think our manuals should mention this more clearly, I will reopen this as a docs issue.

    And I'm already working on it :)

  7. reopened this on Jan 8, 2020
  8. self-assigned this
    on Jan 8, 2020
  9. added
    status: wip
    docs
    on Jan 8, 2020
  10. removed
    status: wip
    on Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsquestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions