Mig*_* M. 2 javascript mysql node.js sequelize.js
我正在使用Sequelize ORM和Node.js,我无法理解如何用它构建以下查询.
SELECT service.*, serviceCollection.*, client.companyName, client.idclient
FROM service
INNER JOIN serviceCollection
ON service.serviceCollection_idserviceCollection = serviceCollection.idserviceCollection
INNER JOIN client
ON serviceCollection.client_idclient = client.idclient
Run Code Online (Sandbox Code Playgroud)
当我在phpMyAdmin上尝试时,此查询可以正常运行.复杂的是客户端模型与Service没有直接关系,因此在尝试执行此操作时:
Service.findAll({include : [ServiceCollection, Client]}).then(function(service){
if(service) {
res.status(200).json(service);
}
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Unhandled rejection Error: client is not associated to service!
Run Code Online (Sandbox Code Playgroud)
我如何准备查询的方式是从serviceCollection中的foreignkey获取客户端信息,而不是原始模型 - service.我不能只include在客户端使用,因为它会给我错误,我需要将其与其他方式的查询相关联,我似乎无法在文档中找到解决方案.
Jan*_*ier 10
您是否在servicecollection和客户端之间建立了关系?在这种情况下,你可以这样做:
Service.findAll({
include : [
{
model: ServiceCollection,
required: true,
include: [{model: Client, required: true }]}
]
});
Run Code Online (Sandbox Code Playgroud)
required: true 创建内部联接,默认为左联接