Sequelize包含在哪里

时间:2015-11-04 16:57:38

标签: mysql node.js join where sequelize.js

我使用sequelize作为我的后端ORM。 但是当我想要"其中"使用连接表。协会很好,但我不知道怎么做"其中"。

这是我的代码:

router.get('/id_presta_struct_unit/:id_presta_struct_unit', (req, res) => {
  models.structures.findAll({
    include: {
      required: false,
      model: models.structures_proposer_prestations,
      where: {
        id_presta_struct_unit: req.params.id_presta_struct_unit
      },
      include: {
        model : models.unites_facturation,
      }
    }
  }).then(data => {
    res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
    res.end(JSON.stringify(data));
  });
});

我收到了这个请求

SELECT * FROM structures AS structures LEFT OUTER JOIN structures_proposer_prestations AS structures_proposer_prestations ON structuresid_structure = structures_proposer_prestationsid_structure structures_proposer_prestationsid_presta_struct_unit =' 1' LEFT OUTER JOIN unites_facturation AS structures_proposer_prestations.unites_facturation ON {{ 1}}。structures_proposer_prestations = id_unitestructures_proposer_prestations.unites_facturation;

但我想得到

SELECT * FROM id_unite AS structures LEFT OUTER JOIN structures AS structures_proposer_prestations ON structures_proposer_prestationsstructures = id_structurestructures_proposer_prestations LEFT OUTER JOIN id_structure AS unites_facturation ON structures_proposer_prestations.unites_facturationstructures_proposer_prestations = id_unitestructures_proposer_prestations.unites_facturation WHERE {{1 } {。{1}} =' 1' ;

我不知道该怎么办我找不到有同样问题的帖子

有人能指出我正确的方向吗?

提前谢谢。

编辑:

协会

id_unite

资源模型

structures_proposer_prestations

和structures_employer_ressources的模型

id_presta_struct_unit

1 个答案:

答案 0 :(得分:2)

如果将数组提供给初始连接的where子句,则可以对连接运行原始查询。

示例:

 models.structures.findAll({
where:[["[structures_proposer_prestations].[id_presta_struct_unit] = " + req.params.id_presta_struct_unit, null]],
    include: {
      required: false,
      model: models.structures_proposer_prestations,
      where: {
        id_presta_struct_unit: req.params.id_presta_struct_unit
      },
      include: {
        model : models.unites_facturation,
      }
    }
  }

数组也可以采用标准对象语法,并由AND组合。我传入的null是for paramaters,所以它绝对可以优化为id作为参数,只是不知道手边的语法。