Sequelize exclude belongs-to-many mapping object

可紊 提交于 2019-12-04 10:43:14

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
        }
    ]
});

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!