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

Sort by non selected fields #969

Open
wisac opened this issue Sep 5, 2024 · 2 comments
Open

Sort by non selected fields #969

wisac opened this issue Sep 5, 2024 · 2 comments

Comments

@wisac
Copy link

wisac commented Sep 5, 2024

In order to sort by a field, the field must be selected. This means sorting depends on selection.

Example ?sortBy=createdAt:DESC&select=name will throws a column distinctAlias.__root_name does not exist error even though createdAt is included in sortableColumns.

Ideally, I want to be able to sort by a field but not to return that field.
Eg. sort by createdAt but not return createdAt.

Currently, I'm using a dto to get rid of the field I used for sorting and don't want to select it but I think it would be great to achieve this out of the box.
Is there a way, am I missing something. I'm using postgres

@Helveg
Copy link
Collaborator

Helveg commented Sep 5, 2024

Hey! Thanks for your report. You are correct, the select mechanisms works rather "aggressively" on the database level: select=col1 will only SELECT (col1) FROM! This means that columns not included in the select simply do not exist to perform any querying on, and this is where your error comes from.

We ought to add any column we need to operate on to the columns we select.

This would solve your error, but then the not-selected-yet-queried column will appear in the response. With some additional logic we could either create a subquery, and then select only the truly selected columns from our subquery; or we could strip the unselected fields from the database response on the JS level.

Not sure what the best solution would look like.

Is this something you could draft a PR for? Perhaps the best way to begin tackling this is if you could open a PR with a test case that fails on this error? :)

@natoszme
Copy link

Hi @Helveg! Is this related to the fact that when using filterableColumns with associations, the associations must be present in the relations array? Or should I open another issue?
My goal is also to be able to filter by fields not returned in the query.

Thanks!

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

3 participants