How can use LEFT JOIN at Sequelize?

前端 未结 2 901
走了就别回头了
走了就别回头了 2020-12-14 17:17

Relations:

Shop.hasMany(ShopAd, {foreignKey : \'shop_id\', as : \'ads\'});
ShopAd.belongsTo(Shop, {foreignKey : \'id\'})

I using :

相关标签:
2条回答
  • 2020-12-14 17:37

    required: true forces an INNER JOIN, use required: false to force a LEFT JOIN.

    Taken from here.

    0 讨论(0)
  • 2020-12-14 17:41

    using: required:false

    sentences:

    Shop.findAll({
         where:{id:shopId}, 
         include:[
             { model:ShopAd, as:'ads', 
               where:{ 
                     is_valid:1, 
                     is_vertify:1},   
               required:false
               }
             ]
          })
          .success(function(result) {
            callback(result);
        });
    
    0 讨论(0)
提交回复
热议问题