在sails中我需要函数来通过引用的表获取行。像
这样的东西Child.find({ parent.age: 30 })
或SQL语言
SELECT child.* FROM child JOIN parent ON child.parent_id = parent.id WHERE parent.age = 30
到目前为止,我已经编写了uggly函数,它分三个步骤完成工作:
get_children_in_celebration: function(req, res) {
// 1. Get parend ids
Parent.find({ select: ['id'], age: 30}).exec(function(err_parents, res_parents) {
// 2. Collect them to array suitable for next query
var parent_ids = [];
for(var i = 0; i < res_parents.length; i++) {
parent_ids[i] = res_parents[i].id;
}
// Get their children
Child.find({ parent_id: parent_ids }).exec(function(err_children, res_children) {
return res.json(res_children);
});
});
},
第一个查询返回~5000个父项,因此它一起表示db的重载。帆会提供更好的解决方案吗?
答案 0 :(得分:1)
不,Sails不支持对关联表进行过滤。我有类似的问题,使用了原始查询。
Model.query("select * from table", function (error, response) {})