2015-04-27 38 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); 
});