【问题标题】:Foreign key conditions in sailsjs Waterline ORMSailsjs Waterline ORM中的外键条件
【发布时间】:2014-07-01 04:48:55
【问题描述】:

我已经定义了两个模型,如下所示,我需要做的是检索属于活跃合作伙伴的唯一社区(partner.status=1),我可以像这样检索分组的社区

Locations.find({groupBy: [ 'neighborhood' ],sum['id']}, function(err,locations){}); 

然后与检索到的活动合作伙伴列表匹配或向后匹配(即首先获取具有每个合作伙伴位置的活动合作伙伴,并在验证它们尚未在其中后将它们推送到数组)或仅通过自定义 sql 查询(我试图远离) 但是...我想知道是否有某种方法可以使用 ORM 来做到这一点,因为我已经看到 model.find().populate('model') 不接受超出的参数所需的模型不是 find 方法中的 where 接受外键条件。

     Partner:{
        attributes:{
         businessName:{
             type:'string'
             },
         status:{
             type:'INT'
          }
          locations:{
              model:'Locations'
              via:'partner_id'
           }
          }
      }
Locations:{
    attributes:{
         partner_id:{
             model:'Partners',
             type:'INT'
             },
         neighborhood:{
             type:'STRING'
          }
          }
      }

提前致谢!

【问题讨论】:

  • 您找到解决方案了吗?如果是这样,回答你自己的问题会很有趣,因为很多人(比如我)都在寻找这个问题!

标签: javascript sails.js waterline


【解决方案1】:

这是你要找的吗?

Locations.find({where: {value1: key1}})
 .populate('partners', {where: {value2: key2}})
 .exec(function(err, locations){
     // Now you've got what you need
 });

P/S:请查看 GitHub 上的 SailsJS v0.10.x 文档。我个人使用这个:https://github.com/balderdashy/sails-docs/tree/0.10 但是这些文档不是最新的:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2013-12-21
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多