Sequelize exclude belongs-to-many mapping object

前端 未结 2 2164
迷失自我
迷失自我 2021-02-14 23:51

Is there a way when making a SequelizeJS query on an object, and including a relation which has a belongs-to-many association, to have the included property not return the assoc

相关标签:
2条回答
  • 2021-02-14 23:55

    Found a solution to this in a github comment: https://github.com/sequelize/sequelize/issues/4074#issuecomment-153054311

    gist:

    Users.findAll({
        include: [
            {
                model: Role, 
                as: 'roles',
                through: {attributes: []} //<-- this line will prevent mapping object from being added
            }
        ]
    });
    
    0 讨论(0)
  • 2021-02-15 00:04

    You can try specifying the attributes property.

    So to include fields say a,b,c you add

    attributes: ['a', 'b','c']
    

    To exclude them

    attributes:{exclude:['a', 'b','c']}
    

    So findAll looks like

    Model.someModel.findAll({
      where: // some condition
      include: // some other model
      attributes: // some fields
    })
    

    You can also specify attributes within the include clause

    0 讨论(0)
提交回复
热议问题