【问题标题】:Sails query by joined table通过连接表进行 Sails 查询
【发布时间】:2017-01-31 12:31:44
【问题描述】:

在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 的负载很重。帆提供更好的解决方案吗?

【问题讨论】:

  • 很遗憾没有...

标签: orm sails.js


【解决方案1】:

不,Sails 不支持对关联表进行过滤。我遇到了类似的问题,使用了相同的原始查询。

Model.query("select * from table", function (error, response) {})

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 2012-08-29
    • 2020-10-05
    • 1970-01-01
    • 2022-11-11
    • 2019-01-26
    • 2016-08-06
    • 2013-12-06
    • 2020-03-24
    相关资源
    最近更新 更多