queryBuilder fails when using LENGTH keyword in orderBy with leftJoin and take
See original GitHub issueIssue Description
When you use LENGTH keyword inside orderBy
method with leftJoin
and take
methods then it throws an error:
"LENGTH(u" alias was not found. Maybe you forgot to join it?
This is some weird behaviour that I cannot explain.
Expected Behavior
To not throw an error and to return results.
Actual Behavior
const sortedUsers = userRepo
.createQueryBuilder("u")
.leftJoinAndSelect("u.permissions", "p")
.take(2)
.orderBy("LENGTH(u.email)", "DESC")
.getMany();
When you only remove .leftJoinAndSelect("u.permissions", "p")
then it works.
When you only remove .take(2)
then it works.
Also when you remove only LENGTH
keyword then it works with both of them.
But when you use both of them and LENGTH
keyword as in the example above then it throws an error:
"LENGTH(u" alias was not found. Maybe you forgot to join it?
Steps to reproduce
https://github.com/mimoid-prog/typeorm-length-bug Install dependencies and npm start
My Environment
Dependency | Version |
---|---|
Operating System | Windows 10 |
Node.js version | v14.16.0 |
Typescript version | v4.1.3 |
TypeORM version | v0.2.31 |
Additional Context
Relevant Database Driver(s)
-
postgres
Are you willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time, and I know how to start.
- Yes, I have the time, but I don’t know how to start. I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Can't use take and skip alongside order by in querybuilder ...
I noticed that in all the queries I try to do that involve any type of join , the orderBy statement can't be...
Read more >Symfony querybuilder orderby failed - query builder
SELECT user FROM User LEFT JOIN user.groups group ORDER BY group.id ASC, group.name DESC;. So that the result will be ordered by group.id ......
Read more >The QueryBuilder - Doctrine Object Relational Mapper (ORM)
The QueryBuilder. A QueryBuilder provides an API that is designed for conditionally constructing a DQL query in several steps. It provides a set...
Read more >Database: Query Builder
The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported...
Read more >Select using Query Builder - typeorm - GitBook
How to create and use a QueryBuilder ; SelectQueryBuilder · SELECT queries. Example: ·.createQueryBuilder() .select("user") ; InsertQueryBuilder · INSERT queries.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi!
I’ve gotten the error some weeks ago and I found a workaround if you’re interested :
Using LENGTH in addSelect and ordering by the alias doesn’t throw any error.
@juliengbt Thanks, your workaround works! But I needed to change the alias name because I got another error
column distinctAlias.namelength does not exist
. It looks like Typeorm doesn’t like camel case either 😄