如何查找未与 Waterline ORM 连接的记录

How to find records that are not joined with Waterline ORM

我正在为类似 Tinder 的项目使用 node.js 框架、Sails JS 及其内置的 ORM Waterline。

我不知道如何编写查询来获取未连接的记录。在我的例子中,当一个用户 'likes' 或 'dislikes' 另一个用户的个人资料时,我希望喜欢的个人资料不再出现。

我该如何做到这一点?这是我到目前为止尝试过的:

Cat.query('select cat.* from cat left outer join vote on cat.id = vote.owner where vote.owner is null AND cat.id !='+req.session.User.id, function (err, results) {
        if (err) return res.serverError(err);
        res.send('Yes');
        console.log(results);
    });

如果我没看错,Cat 是用户个人资料吗? 因此,我们需要配置文件:

  1. 不属于当前用户(cat.id !== req.session.User.id
  2. 当前用户不喜欢或不喜欢。

    var query = 'select cat.* ' + 
    ' from cat left join vote on' + 
      // joining votes for current user
      ' cat.id = vote.catId and vote.owner = ' + req.session.User.id +
    ' where' + 
    // filtering out the Cat with current user Id (if needed)
    ' cat.id <> ' + req.session.User.id +
    ' and' + 
    // leaving only records with empty vote id(meaning there's no corresponding vote record)
    ' vote.id is null';
    

纯 SQL 更清晰的视图:

select cat.* from cat left join vote on 
  cat.id = vote.catId and vote.owner = :userId
where 
  cat.id <> :userId 
and
  vote.id is null

我们从 cat table 中选择 all 条记录,加入当前用户为它们所做的投票。然后只留下缺少投票的记录,其中 id <> 当前用户 Id