Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用sequilize的include列上的where子句_Node.js_Orm_Sequelize.js - Fatal编程技术网

Node.js 使用sequilize的include列上的where子句

Node.js 使用sequilize的include列上的where子句,node.js,orm,sequelize.js,Node.js,Orm,Sequelize.js,我使用的查询如下所示 db.Spectrum.findAll({ 属性:['title',[db.Sequelize.fn('SUM',db.Sequelize.col('quantity'),'count']], 包括:[ {型号:db.品种, 属性:['specID','id'], 包括:[ {型号:db.Wine, 必填项:false, 属性:['品种id',id'], 地点:{餐厅id:餐厅id, 所有者_id:null} ], } ], 组:['specID'] }) 这给了我正确的

我使用的查询如下所示

db.Spectrum.findAll({
属性:['title',[db.Sequelize.fn('SUM',db.Sequelize.col('quantity'),'count']],
包括:[
{型号:db.品种,
属性:['specID','id'],
包括:[
{型号:db.Wine,
必填项:false,
属性:['品种id',id'],
地点:{餐厅id:餐厅id,
所有者_id:null}
],
}
],
组:['specID']
})

这给了我正确的结果,问题是因为它在左侧连接where子句时运行非常慢(大约2秒)

左连接的原始查询如下所示

左外连接'wines'作为'Varietals.wines'在'Varietals'上的'Varietals.wines'。'id`='Varietals.wines`.'Varietals\u id`和'Varietals.wines`.'restaurant\u id`=16和'Varietals.wines`.'owner\u id`为空

我希望查询具有左join和这样的where子句

左外连接'wines'作为'Varietals.wines'在'Varietals'上。'id`='Varietals.wines`.'varietal\u id`
其中'Varietals.Wines`.'restaurant\u id`=16和'Varietals.Wines`.'owner\u id`为空


但我尝试的每一件事都会在前面加上“光谱”,然后就找不到那一行了。这应该怎么做?还是我只需要使用原始查询

include。其中
将始终向联接添加条件。 但是在Sequelize
v3.13.0
中添加了对特殊嵌套键的支持

现在可以将where对象添加到顶级对象中,该对象包含用$包装的点分隔键,以将其指定为嵌套字段

将其应用于代码:

db.Spectrum.findAll({
  include: [
    {
      model: db.Varietal,
      include : [
        {
          model : db.Wine
        }
      ]
    }
  ],
  where: {
    '$Varietals.Wines.restaurant_id$': restaurant_id,
    '$Varietals.Wines.owner_id$': null
  },
  group : ['specID']
});
(我删除了一些语句以关注重要部分)

Include array Where子句
您需要在属性:[name']中添加所有像name这样的字段(在或中是必需的),否则它将给出unknowm列错误。感谢您让我知道此功能!当我尝试实现它时,虽然它给了我
WHERE`Varietals`.`Wines`.`restaurant\u id`=16
,这给了我“Unknown column”错误,因为它看起来要么需要读取
WHERE`Varietals.Wines`.`restaurant\u id`=16
?啊,你是对的,实际上现在可能很难修复,如果你确实需要在外面的位置。
     include = [{
        model: db.caregiverCategory,
        as: 'categories',
        attributes: ['name'],
        required: true,
        attributes: ['name'],
        include: [{
            model: db.category,
            as: 'category',
            required: true
        }]
    }]
    where[Op.or] = [{
        '$categories.name$': { [Op.like]: '%' + query.category_name + '%' }
   }, {
        '$categories.category.name$': { [Op.like]: '%' + query.category_name + '%' }
    }]