使用条件对连接查询进行序列化
Sequelize join query with condition
我正在使用以下语法来 select 一些记录:
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` = 'xyz@gmail.com';
^^^^^^^^^^^^^^^^^^^^^^
所以它给我一个错误:Unknown column 'account.user.email' in 'where clause'
。我只想 user.email
在 WHERE 子句中。预期的查询应该是:
SELECT *
FROM `accounts` AS `account`
LEFT OUTER JOIN `users` AS `user`
ON `account`.`user_id` = `user`.`id`
WHERE `account`.`password` = 'PASSWORD'
AND `user`.`email` = 'xyz@gmail.com';
我做错了什么?
将用户 table 的 where 过滤器放入包含部分:
models.account.findAll({
attributes: ['password'],
include: [{
model: models.user,
where: {
email: email
}
}],
where: {
password: password
}
}).then(function(res){
console.log(res);
});
我正在使用以下语法来 select 一些记录:
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` = 'xyz@gmail.com';
^^^^^^^^^^^^^^^^^^^^^^
所以它给我一个错误:Unknown column 'account.user.email' in 'where clause'
。我只想 user.email
在 WHERE 子句中。预期的查询应该是:
SELECT *
FROM `accounts` AS `account`
LEFT OUTER JOIN `users` AS `user`
ON `account`.`user_id` = `user`.`id`
WHERE `account`.`password` = 'PASSWORD'
AND `user`.`email` = 'xyz@gmail.com';
我做错了什么?
将用户 table 的 where 过滤器放入包含部分:
models.account.findAll({
attributes: ['password'],
include: [{
model: models.user,
where: {
email: email
}
}],
where: {
password: password
}
}).then(function(res){
console.log(res);
});