16

I have a sequelize query from multiple tables inner joined together. I need to group them by on a nested include model but the sequelize query throws the primary key every time, even if I mention the attributes as: attributes:[].

However attributes:[] is working for nested include models.

1
  • 1
    Looks like the id is added because Sequelize is trying to pull in that model. I am trying to accomplish the same thing as you but haven't been able to figure out the solution using findAll.
    – JCF
    Aug 1, 2019 at 14:38

4 Answers 4

40

You can exclude any attributes by passing an exclude array into the attributes option:

MyModel.findAll({
  attributes: {exclude: ['some_field']}
});
2
  • 5
    Doesn't work for the primary key in this case. I have a similar query I'm trying to achieve and listing the primary key in the exclude array has no effect.
    – JCF
    Aug 1, 2019 at 14:40
  • @JCF Downvote? Can you confirm this in the Sequelize docs or code base? I'm looking at the code and don't see where it disallows exclude a primary key. Aug 1, 2019 at 15:08
5

For included models use attributes: ['prop_name']

Remember include/exclude will not affect nested tables use through: { attributes:[]}

Model.addScope('scope_name', {
      attributes: ['id', 'name'],
      include: [
        {
          model: models.role,
          as: 'roles',
          attributes: ['name'],
          through: {
            attributes: []
          }
        }
      ]

More details can be found here: https://github.com/sequelize/sequelize/issues/4074#issuecomment-153054311

2

I want to add that you can explicitly list the attributes you want and that they work on nested inner joins as follows:

const my_model = await MyModel.findById(id, {
  include: [
    {
      model: AnotherModel,
      attributes: [ 'displayName', 'email' ] // only these attributes returned
    },
    { model: YetAnotherModel,
      include: [{
        model: AnotherModel,
        attributes: [ 'id', 'displayName', 'email' ]
      }]
    }
  ]
})

Your returned Object should look like:

{
  // ...MyModel attributes
  ,
  AnotherModel: {
    displayName: '...',
    email: '...',
  },
  YetAnotherModel: {
    // ...YetAnotherModel's attributes
    ,
    AnotherModel: {
      id: '...',
      displayName: '...',
      email: '...',
    }
  }
}
0

For those who still are looking for the solution to this problem. From https://github.com/sequelize/sequelize/pull/4029, if you add raw: true to the query, it removes the primary key.

Model.findOne({ 
  attributes: [
    [Sequelize.fn('SUM', Sequelize.col('field')), 'sum'],
  ],
  raw: true
})

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.