1

我有这个查询:

model.findAndCountAll({
  attributes: ['FirstName', 'LastName'],
  include: [{
    model: models['hobbies'],
    as: 'Hobbies',
    attributes: ['ID', 'Name'],
    where: {
      [Sequelize.Op.and]: [
        { 'ID': 1 },
        { 'ID': 2 }
      ]
    }
  }]
})

我试图让一名员工具有查询中指定的两个爱好,尽管它返回 0 行。AND 运算符在应用于同一列时似乎不起作用。有没有办法处理这种查询?

员工表数据:

 EmployeeID | FirstName | LastName | HobbyID
------------+-----------+----------+----------
          1 | John      | Doe      | 1
          1 | John      | Doe      | 1
          2 | Jane      | Doe      | 1

员工表关联:

model.hasMany(models['Hobbies'], { as: 'Hobbies', foreignKey: 'EmployeeID', targetKey: 'EmployeeID' });

输出:

{
  count: 0,
  rows: []
}

预期输出:

{
  FirstName: "John",
  LastName: "Doe",
  Hobbies: [
    { Name: "Sports - Basketball" },
    { Name: "Sports - Volleyball" }
  ]
}

并且它应该始终返回具有指定的两种爱好的员工。

4

1 回答 1

0

你可以试试这个

model.findAndCountAll({
  attributes: ['FirstName', 'LastName'],
  include: [{
    model: models['hobbies'],
    as: 'Hobbies',
    attributes: ['ID', 'Name'],
    where: {
        ID: [1,2]
      ]
    }
  }]
})
于 2020-11-17T09:46:23.987 回答