question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

queryBuilder fails when using LENGTH keyword in orderBy with leftJoin and take

See original GitHub issue

Issue 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:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
juliengbtcommented, Jul 6, 2021

Hi!

I’ve gotten the error some weeks ago and I found a workaround if you’re interested :

const users4 = await userRepo
    .createQueryBuilder("u")
    .addSelect("LENGTH(u.name)", "nameLength")
    .leftJoinAndSelect("u.pets", "p")
    .orderBy("nameLength", "DESC")
    .take(2)
    .getMany()

Using LENGTH in addSelect and ordering by the alias doesn’t throw any error.

2reactions
mimoid-progcommented, Jul 6, 2021

@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 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found