Skip to content
New issue

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

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

Already on GitHub? # to your account

Using CASE in orderBy leads to alias not found error #1013

Open
jlefebvre1997 opened this issue Oct 23, 2024 · 2 comments
Open

Using CASE in orderBy leads to alias not found error #1013

jlefebvre1997 opened this issue Oct 23, 2024 · 2 comments

Comments

@jlefebvre1997
Copy link

Hello there 👋

I'm using nestjs-paginate for my project, and as far as it goes everything is perfect, except this one thing : I'm trying to sort my data by a specific column, but I want a specific value to always be placed first, for example :

.addOrderBy(
        `CASE 
           WHEN content.type = :folder THEN 1 
           WHEN content.type = :course THEN 2 
           WHEN content.type = :event THEN 3
           ELSE 3 
         END`,
        'ASC',
      )

However, this fails with this error : WHEN content" alias was not found. Maybe you forgot to join it?, even though I did provide the content alias when calling createQueryBuilder. Any idea on why this happens ?

Thanks for your time and help 🙏

@jlefebvre1997
Copy link
Author

What I don't understand is that the generated SQL queries are absolutely identical (apart from the defaultSortBy that gets added at the end but even if I remove it it still fails)

@jlefebvre1997
Copy link
Author

I fixed it by using addSelect instead of orderBy and ordering by the column I created :

        `CASE 
           WHEN content.type = :folder THEN 1 
           WHEN content.type = :course THEN 2 
           WHEN content.type = :event THEN 3
           ELSE 3 
         END`,
        'type_order',
      )
      .addOrderBy('type_order', 'ASC')

Seems strange that this work but orderingb by with Case directly doesn't

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

No branches or pull requests

1 participant