mysql - Sequelize 在查询中返回连接表

标签 mysql node.js sequelize.js

我的 MSQL 表中这两个模型之间存在多对多关系:

  • Venue - Representing a venue which can have multiple owners (employees)
  • Employee - Representing an employee which can be either a ceo or sales employee or whatsoever.

我正在使用 像这样建立关系:

关系员工>地点

 Employee.associate = function (models) { 
   models.Employee.belongsToMany(models.Venue, { through: 'EmployeeVenues' })
 }

关系场所>员工

 Venue.associate = function (models) {
    models.Venue.belongsToMany(models.Employee, { through: 'EmployeeVenues' })
 }

Sequelize 文档

According to the Sequelize docs it will create a new model called EmployeeVenues with the equivalent foreign keys employee_id and venue_id. Defining through is required. Sequelize would previously attempt to autogenerate names but that would not always lead to the most logical setups. This will add methods getVenues, setVenues, addVenue, addUsers to Employee.


这是正常工作的,当我启动我的 Sequelize 时,它​​会创建一个名为 EmpoyeeVenues 的新表,并将正确的外键作为复合键。但是,当我查询 getVenues 时,它不会返回预期的输出。相反,它也返回关联的表值,这是我不想要的。


查询以获取属于 id 等于 1 的员工的所有场所


router.get('/api/v1/users/:employee_id/venues', (request, response) => {
  var employeeId = request.params.employee_id;

  models.Employee.findOne({
    where: {id: employeeId}
  }).then((employee) => {

    if(!employee) { return response.status(400).send("Employee doesnt have a venue registered yet.")}
    var venues = employee.getVenues().then((venues) => {
    response.status(200).send(venues);
  })
})
});

响应结果


[
    {
        "id": 1,
        "capacity": "11",
        "venue_name": "Club Fix",
        "venue_description": "Club in Tilburg",
        "EmployeeVenues": {
            "employee_id": 1,
            "venue_id": 1
        }
    },
    {
        "id": 2,
        "capacity": "400",
        "venue_name": "Club Vie",
        "venue_description": "Club in Rotterdam",
        "EmployeeVenues": {
            "employee_id": 1,
            "venue_id": 2
        }
    }
]

问题

Why is EmployeeVenues included in this query provided by Sequelize? And How can I prevent EmployeeVenues to be included in the response?


更新

According to a issue on the Sequelize github page which was made in 2014 there is a solution that works

https://github.com/sequelize/sequelize/issues/2143

    User.find({
    where: {id: userId}, attributes: userFields,
    include: [
      {model: db.Role, attributes: roleFields, through: {attributes: []}}
    ]
});

但它与最新的 Sequelize 文档中记录的版本不匹配,至少它应该是最新的。


User.findAll({
  include: [{
    model: Project,
    through: {
      attributes: ['createdAt', 'startedAt', 'finishedAt'],
      where: {completed: true}
    }
  }]
});

或者甚至在 reference documentation: 上简单说明


user.getPictures() // gets you all pictures

最佳答案

更新

According to a issue on the Sequelize github page which was made in 2014 there is a solution that works

https://github.com/sequelize/sequelize/issues/2143

    User.find({
    where: {id: userId}, attributes: userFields,
    include: [
      {model: db.Role, attributes: roleFields, through: {attributes: []}}
    ]
});

但它与最新的 Sequelize 文档中记录的版本不匹配,至少它应该是最新的。


User.findAll({
  include: [{
    model: Project,
    through: {
      attributes: ['createdAt', 'startedAt', 'finishedAt'],
      where: {completed: true}
    }
  }]
});

或者甚至在 reference documentation: 上简单说明


user.getPictures() // gets you all pictures

关于mysql - Sequelize 在查询中返回连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47961046/

相关文章:

node.js - 在 WebStorm 中将 Node.js 与 Flow 结合使用

javascript - NodeJS 中的事件队列

node.js - Sequelize : Only returning one of many result for a nested include

phpMyAdmin 在我的 apache httpd 上显示空白页面

javascript - 在 Node.js 中使用 .bin 文件内容填充类型化数组

node.js - 如何保存数组?

mysql - 当一条路由当前正在向数据库插入大量数据时,如何使所有其他路由正常工作?

php - 如何自动填充文本框取决于从下拉列表中选择选项

php - 我现在已经浏览了所有论坛,但仍然无法让我的 Php 将数据发送到我的数据库以供评论系统

MySql 将列中的所有字段更新为其默认值