Skip to content

Hide eager loaded n:m associations #5078

Closed

Description

@wadebiffen

I have a requirement to get all "Roles" associated with a specified "User" where the output does not include any associations and restrictions added to the model definitions (e.g. not soft-deleted) should automatically be included in the generated SQL.

I would have thought that the following code should have solved it for me;

Role.findAll({
  include: [{
    model: User,
    where: { id: 1 },
    required: true,
    through: { attributes: [] },
    attributes: [],
  }]
}).then((instances) => {
    console.log(instances);
});

Maybe I am going about this the wrong way, but this results in:

'TypeError: Cannot set property \'User_Roles\' of undefined',
     '    at groupJoinData (d:\\test\\node_modules\\sequelize\\lib\\dialects\\abstract\\query.js:364:37)',

Going to query.js:364 and changing the line to: if ($parent) $parent[$lastKeyPrefix] = resultMap[itemHash] = values; solves it and I get the desired output as described below.

Working code, but gives the wrong output:

Role.findAll({
  include: [{
    model: User,
    where: { id: 1 },
    required: true,
    through: { attributes: [] },
    attributes: ['id'],
  }]
}).then((instances) => {
    console.log(instances);
});

Current output:

[
  {
    "id": 1,
    "name": "Administrator",
    "createdAt": "2015-12-20T13:40:59.224Z",
    "updatedAt": "2015-12-20T13:40:59.224Z",
    "deletedAt": null,
    "Users": [
      {
        "id": 1
      }
    ]
  }
]

Desired output:

[
  {
    "id": 1,
    "name": "Administrator",
    "createdAt": "2015-12-20T13:40:59.224Z",
    "updatedAt": "2015-12-20T13:40:59.224Z",
    "deletedAt": null
  }
]

Is there a better way of solving this that does not include modifying sequelize files? I thought of querying directly on Role, but I need the inner join to User using sequelize association to get an empty result when the User shouldn't be found (e.g is deleted.).

Edit: Using Sequelize v3.15.0

Activity

  1. added
    type: bug
    on Dec 21, 2015
  2. mickhansen commented on Dec 21, 2015

    @mickhansen
    Contributor

    Looks like there's an issue with nested attributes: []

  3. abhisheksaraiya commented on Jan 23, 2016

    @abhisheksaraiya

    I would like to confirm that this had been working for a deprecated version specifically 3.7.1 but today when I updated to 3.18.0, I get the exact same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions