关联表查询代码如下:
const data = await Task.findAll({
include: [{
model: dept,
as: 'department',
attributes: [
['id', 'departmentID'],
['department_name', 'departmentName']
]
}]
})
结果如下
"department": [
{
"departmentID": 1,
"departmentName": "部门1",
"dptrule": {
"dptID": 1,
"ruleID": 1
}
},
{
"departmentID": 2,
"departmentName": "部门2",
"dptrule": {
"dptID": 2,
"ruleID": 1
}
}
]
dptrule 是department表和rule表的关联表
models.rules.belongsToMany(models.dept, {
as: 'department',
through: models.dptrule,
foreignKey: 'ruleID',
constraints: false
})
怎么可以查询结果去掉dptrule, 用attributes.exclude,没效果,求教各位了,谢谢
使用through 可以解决这个问题,链接