Bookshelf.js - Nested relations while querying with andWhere()

99封情书 提交于 2021-01-29 11:11:35

问题


I'm using the strapi framework based on node.js and while creating a query-builder, I'm facing an issue with the andWhere() clause.

I am implementing search functionality where a user can input some text in a form input field, which gets passed in, and database table fields are queried based on that text.

In the code below, you can see that I query for two things:

  1. Search the product model wherever the text is present in the name field.
  2. And always check and search the product model wherever the company field contains the value stored in usercompany variable - let us assume to be "Apple".

OK, so basically I have multiple user roles and I want to add another condition to this. I'm trying to use the dot (.) method to access nested relations in a query, but this doesn't seem to work: qb.andWhere('createdby.role', role) where the role will be the value of the current logged in user, a numerical field where each role id is mapped to some string value like 'Admin' or something in the database already. However, this results into an error that doesn't allow me to query nested relations. I don't have much experience with bookshelf so IDK how to solve this.

Code:

var test = await strapi
      .query("product")
      .model.query((qb) => {
        qb.where(function () {
          this.where("name", "LIKE", "%" + searchterm + "%");
        });
        qb.andWhere("company", usercompany);
        qb.andWhere("createdby.role", role); // Here, role = 8
      })
      .orderBy("updated_at", "desc")
      .fetchAll();

Error:

error Error: Undefined binding(s) detected when compiling SELECT. Undefined column(s): [createdby] query: select `product`.* from `product` where (`name` 
like ?) and `company` = ? and `createdby` = ? order by `product`.`updated_at` desc

Hoping for a reply as soon as possible, thanks!

来源:https://stackoverflow.com/questions/63243138/bookshelf-js-nested-relations-while-querying-with-andwhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!