gpt4 book ai didi

javascript - filter Sequelize belongsToMany get 关联表

转载 作者:行者123 更新时间:2023-11-29 01:26:08 24 4
gpt4 key购买 nike

我正在使用 Sequelize 4.37.10,它运行良好。不幸的是,我认为文档并不完美。所以它缺少一点描述 belongsToMany 的可能性。

我有以下问题:

我这样定义我的表:

const Department = db.define('department', {
name: {type: Sequelize.STRING, allowNull: false},
shortName: {type: Sequelize.STRING, allowNull: false}
})

const Employee = db.define('employee', {
title: {type: Sequelize.STRING},
name: {type: Sequelize.STRING, allowNull: false},
surname: {type: Sequelize.STRING, allowNull: false},
.
.
role: {type: Sequelize.STRING}
})

然后我像这样关联表:

const EmployeeDepartments = db.define('employeeDepartments', {
manager: {type: Sequelize.BOOLEAN, allowNull: false}
})

Department.belongsToMany(Employee, {through: EmployeeDepartments})
Employee.belongsToMany(Department, {through: EmployeeDepartments})

现在我想让所有部门员工的 manager 字段都设置为 true。创建没有问题,但选择对我来说是个问题。

我尝试了以下但没有成功:

department.getEmployees({where: {manager: true}})

我也想到了作用域,但我不知道如何正确设计它。

你能帮我吗?

最佳答案

有趣的是,我只是在寻找完全相同的东西,唯一的结果是你几个小时前的问题......

我现在已经解决了,你需要的是:

department.getEmployees({ through: { where: { manager: true } } })

您还可以获取您只有id部门的所有员工:

const department = Department.build( { id: departmentId } );
// proceed as above

关于javascript - filter Sequelize belongsToMany get<Association> 关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50921788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com