filter Sequelize belongsToMany获取<association>关联表

时间:2018-06-19 06:21:42

标签: javascript mysql sequelize.js has-and-belongs-to-many

我正在使用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})

现在我希望将所有部门员工的经理字段设置为true。 创作没问题,但选择对我来说是一个问题。

我试了以下但没有运气:

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

我也考虑过范围,但我不知道如何正确设计。

你能帮帮我吗?

3 个答案:

答案 0 :(得分:3)

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

我现在已经解决了,您需要的是以下内容:

myTextFrame.WrapFormat.Style = WrapStyle.Through;

您还可以获取department.getEmployees({ through: { where: { manager: true } } }) 中只有department的所有员工:

id

答案 1 :(得分:2)

module.exports = function(RED) {
    function expIn(config) { 
        //some code
    }
}

用此更改此代码。

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

您只需在查询中添加包含

答案 2 :(得分:0)

搜索了一段时间后,我自己找到了一个非常简单的答案。

使用belongsToMany时,Sequelize确实使用联接从第二个表中获取数据(我知道)。

我可以选择使用以下语法:

department.getEmployees({where: {'$employeeDepartments.manager$': true}})

我在Google的某个网页上发现了该网址,但不幸的是我丢失了该网址。 也许这可以帮助遇到类似问题的人。

相关问题