使用 knex 查询按关系过滤项目

发布于 2025-01-11 01:27:09 字数 1120 浏览 0 评论 0原文

我正在尝试使用 knex 查询按关系过滤项目。我已经快到了(我想),但有点挣扎,需要一些帮助,因为这对我来说是新的。

我有一个正在关注人和有关注者的用户列表。我正在尝试返回我尚未关注的用户列表。下面是到目前为止我的代码:

const users = await knex("users-permissions_user").whereNotExists(
  function () {
    this.select("*")
      .from("users_followers__users_followings")
      .where("user_id", "users-permissions_user.id")
      .where("follower_id", id);
  }
);

这将返回当前没有关注者的用户列表以及我是唯一关注者的用户的列表。我关注并且拥有更多关注者的任何用户仍会返回。我认为 like 可以实现这种类型的过滤器,但我一定做错了。

以下是关注者/关注关系表在我的数据库中的显示方式: 输入图片这里的描述

这是从上面的查询返回的数据:

[
  {
    "id": "138",
    "followers": [
      {
        "id": "143"
      }
    ]
  },
  {
    "id": "140",
    "followers": [
      {
        "id": "160"
      },
      {
        "id": "136"
      }
    ]
  },
  {
    "id": "135",
    "followers": []
  },
  {
    "id": "136",
    "followers": []
  }
]

如您所见,没有关注者的用户被返回,我还没有关注但有多个关注者的用户也是如此,包括我(ID 160),当它们应该被省略时被返回。

任何建议将不胜感激!

I'm trying to filter items by relation using a knex query. I'm almost there (I think) but struggling a little and could use some help as this is new to me.

I have a list of users who are following people and have followers. I'm trying to return a list of users who I'm not already following. Below is my code so far:

const users = await knex("users-permissions_user").whereNotExists(
  function () {
    this.select("*")
      .from("users_followers__users_followings")
      .where("user_id", "users-permissions_user.id")
      .where("follower_id", id);
  }
);

This returns a list of users who currently have no followers and users where I'm the only follower. Any users who I follow and also have more followers are still returned. I thought like would achieve this type of filter but I must be doing it wrong.

Here is how the table for the followers/following relation appears in my db:
enter image description here

And here is the data that would be returned from the above query:

[
  {
    "id": "138",
    "followers": [
      {
        "id": "143"
      }
    ]
  },
  {
    "id": "140",
    "followers": [
      {
        "id": "160"
      },
      {
        "id": "136"
      }
    ]
  },
  {
    "id": "135",
    "followers": []
  },
  {
    "id": "136",
    "followers": []
  }
]

As you can see, users with no followers are returned as are users who I'm not already following but users who have multiple followers, including me (ID 160), are returned when they should be omitted.

Any advice would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-01-18 01:27:09

因此,像 140 这样的用户即使关注了您 (160) 也会被返回,因为他们至少关注了一个不是您的其他人,这意味着您的 where 子句将匹配它们。
如果您只想返回未关注您的用户,可以通过将 left joinwhere 子句替换为 where not办法 来实现此目的> 条款。在 knex 中,它看起来像这样:

qb.whereNotExists(function() {
     this.select(1)
    .from('users_followers__users_followings')
    .where('users_followers__users_followings.user_id', knex.ref('users-permissions_user.id'))
    .where('users_followers__users_followings.follower_id', id);
});

So the reason that users like 140 are being returned even though they are following you (160) is because they are following at least one other person who isn't you, which means your where clause will match them.
If you want to return only users who are not following you, you could achieve this by replacing your left join and where clause with a where not exists clause. In knex that would look something like:

qb.whereNotExists(function() {
     this.select(1)
    .from('users_followers__users_followings')
    .where('users_followers__users_followings.user_id', knex.ref('users-permissions_user.id'))
    .where('users_followers__users_followings.follower_id', id);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文