Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$joinRelation adds select * to the query if groupBy is not in the query #177

Open
wz5899 opened this issue Apr 29, 2022 · 1 comment
Open

Comments

@wz5899
Copy link
Contributor

wz5899 commented Apr 29, 2022

I am seeing a strange behavior in feathers-objection. If context.params.query has operator "$joinRelation" but no operator "groupBy", feathers-objection will add (*) in "select" which makes operator $select irrelevant. Is there a reason for this implementation or is this a bug? The version of the feathers-objection I use is 7.5.1.

For example:
I have a model "users" that has a ManyToManyRelation roles with model "Roles"

In a find query,
context.params.query: { "$select": ["email"] }
This will only show the "email" and "id" fields in the model and it works correctly.

If adding $joinRelation,
context.params.query: { "$select": ["email"], "$joinRelation": {"roles": true } }
This will show ALL the fields in the model despite the $select: ["email"].
The SQL query generated has "select users.email, users.id, users.* from...".

I looked into feathers-objection code and found the users.* is added in groupByColumns check:

lib\index.js:
createQuery(params = {}) {
...
    const joinRelation = query && query.$joinRelation;
...
    if (joinRelation) {
      const groupByColumns = this.getGroupByColumns(q);

      if (!groupByColumns) {
        q.distinct(`${this.Model.tableName}.*`);
      }
    }
}

One workaround to show only the fields in $select when using joinRelation is adding a modifier builder.groupBy('users.id'); into the query. This works but feels awkward. Are there any cleaner ways to get around this issue?

@arakir
Copy link

arakir commented Mar 2, 2023

I have similar problem with this. When using $sort with $joinRelation DB throw error not having ORDER BY column in SELECT. Can you at least add a way how to opt out of adding this distinct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants