2015-04-27 36 views
2

我使用以下語法選擇記錄:Sequelize連接查詢與條件

models.account.findAll({ 
    attributes: ['password'], 
    include: [{ 
      model: models.user, 
      as : 'user' 
    }], 
    where: { 
     'password': password, 
     'user.email': email 
     } 
}).then(function(res){ 
    console.log(res); 
}); 

它生成以下查詢:

SELECT * 
FROM `accounts` AS `account` 
     LEFT OUTER JOIN `users` AS `user` 
        ON `account`.`user_id` = `user`.`id` 
WHERE `account`.`password` = 'PASSWORD' 
     AND `account`.`user.email` = '[email protected]'; 
      ^^^^^^^^^^^^^^^^^^^^^^ 

因此,它給我的錯誤:Unknown column 'account.user.email' in 'where clause'。我只需要user.email。預期的查詢如下:

SELECT * 
    FROM `accounts` AS `account` 
      LEFT OUTER JOIN `users` AS `user` 
         ON `account`.`user_id` = `user`.`id` 
    WHERE `account`.`password` = 'PASSWORD' 
      AND `user`.`email` = '[email protected]'; 

我在做什麼錯???

回答

4

把其中過濾器爲用戶表中包括部分:

models.account.findAll({ 
    attributes: ['password'], 
    include: [{ 
    model: models.user, 
    where: { 
     email: email 
    } 
    }], 
    where: { 
    password: password 
    } 
}).then(function(res){ 
    console.log(res); 
});