FindSell WHERE在sequelize中使用include with alias字段

时间:2017-09-12 04:28:39

标签: mysql node.js express sequelize.js

如何使用别名包含关联

对WHERE执行FindAll

现在我有了这段代码:

 Photobook.findAndCountAll({
    include: [
      {
        attributes: ['full_name'],
        as: 'layouter_name',
        model: UserModel,
      },
      {
        attributes: ['full_name'],
        as: 'current_pic',
        model: UserModel,
      }
    ],
    where:{
       {
         '$current_pic.full_name$' : {
             $like: '%name%'
         }
      }
    }
 });

由于Photobook模型没有current_pic.full_name字段,因此无效。我只想搜索current_pic关联而不是layouter_name关联。

如何执行类似的操作?

1 个答案:

答案 0 :(得分:0)

试试这个:

Photobook.findAndCountAll({
    include: [
      {
        attributes: ['full_name'],
        as: 'layouter_name',
        model: UserModel,
      },
      {
        attributes: ['full_name'],
        as: 'current_pic',
        model: UserModel,
        where: {
            full_name: {
                $like: '%name%'
            }
        },
        required: true
      }
    ]
 }); 

您可以在include数组中给出where条件。 required: true确保JOIN被视为INNER JOIN