typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

when useing skip and take include ORDER CASE WHEN, An error occurred for "CASE WHEN alias"

Open JuTiger-Lee opened this issue 5 months ago • 6 comments

Issue description

when useing skip and take include ORDER CASE WHEN, An error occurred for "CASE WHEN alias"

Expected Behavior

I thought that when using the CASE WHEN clause and skip take, I would get results with duplicate removal and guaranteed order, but an error occurred.

Actual Behavior

image When using ordery, there are cases where CASE WHEN and Skip take are used.

Steps to reproduce

const query = this.recruitmentRepo.createQueryBuilder('r')
.orderBy('CASE WHEN r.id IN (:...ids) THEN 1 ELSE 2 END', ASC)
.setParameters({ id: [] })
.skip(0)
.take(10)

When you run the above code, a CASE WHEN alias error occurs.

I think the reason why the error occurs is getMany -> getRawAndEntities executeEntitiesAndRawResults -> createOrderByCombinedWithSelectExpression function in SelectQueryBuilder.js file image

There is a problem with the photo code below:

const criteriaParts = orderCriteria.split("."); // this problem
const aliasName = criteriaParts[0]; // result is "CASE WHEN r"
const alias = this.expressionMap.findAliasByName(aliasName); // error

So, there is no problem if you use Limit offset, but since duplicates are not removed, you must use Skip take. I think this code is buggy. If it's a bug, can I fix it?

My Environment

Dependency Version
Operating System macos
Node.js version 18.18.0
Typescript version 5.1.3
TypeORM version 0.3.20

Additional Context

No response

Relevant Database Driver(s)

  • [ ] aurora-mysql
  • [ ] aurora-postgres
  • [ ] better-sqlite3
  • [ ] cockroachdb
  • [ ] cordova
  • [ ] expo
  • [ ] mongodb
  • [X] mysql
  • [ ] nativescript
  • [ ] oracle
  • [ ] postgres
  • [ ] react-native
  • [ ] sap
  • [ ] spanner
  • [ ] sqlite
  • [ ] sqlite-abstract
  • [ ] sqljs
  • [ ] sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

JuTiger-Lee avatar Sep 23 '24 06:09 JuTiger-Lee