Strange behavior when using `limit` with relations (joins)
See original GitHub issueIssue type:
[ ] question [X] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[X] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[0.2.7] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
Short version
When using joins, limit()
does not seem to work properly.
Long Version
I have the following query:
return this.branchRepo.createQueryBuilder("branches")
.innerJoinAndSelect("branches.branchSchedules", "branch_schedules", "branch_schedules.branch_id = branches.id")
.leftJoinAndSelect("branches.branchFeature", "branchFeature", "branchFeature.branch_id = branches.id")
.leftJoinAndSelect("branchFeature.feature", "features")
.orderBy("branches.is_all_day", "DESC")
.addOrderBy("branch_schedules.end_time::time", "DESC")
.addOrderBy("branch_schedules.day", "DESC")
.addOrderBy(`(case when branch_schedules.day = 'holiday' then 1
when branch_schedules.day = 'sunday' then 2
when branch_schedules.day = 'saturday' then 3
when branch_schedules.day = 'weekday' then 4
end)`,
"ASC")
.addOrderBy("branch_schedules.start_time::time", "ASC")
.limit(5)
.getMany();
Which produces the following query:
SELECT "branches"."estimated_time" AS "branches_estimated_time", "branches"."is_all_day" AS "branches_is_all_day", "branches"."address" AS "branches_address", "branches"."id" AS "branches_id", "branches"."name" AS "branches_name", "branches"."latitude" AS "branches_latitude", "branches"."has_extended_schedule" AS "branches_has_extended_schedule", "branches"."created_at" AS "branches_created_at", "branches"."updated_at" AS "branches_updated_at", "branches"."longitude" AS "branches_longitude", "branches"."code" AS "branches_code", "branches"."phone" AS "branches_phone", "branch_schedules"."end_time" AS "branch_schedules_end_time", "branch_schedules"."id" AS "branch_schedules_id", "branch_schedules"."start_time" AS "branch_schedules_start_time", "branch_schedules"."updated_at" AS "branch_schedules_updated_at", "branch_schedules"."created_at" AS "branch_schedules_created_at", "branch_schedules"."day" AS "branch_schedules_day", "branch_schedules"."branch_id" AS "branch_schedules_branch_id", "branchFeature"."updated_at" AS "branchFeature_updated_at", "branchFeature"."created_at" AS "branchFeature_created_at", "branchFeature"."branch_id" AS "branchFeature_branch_id", "branchFeature"."feature_id" AS "branchFeature_feature_id", "features"."id" AS "features_id", "features"."created_at" AS "features_created_at", "features"."name" AS "features_name", "features"."slug" AS "features_slug", "features"."updated_at" AS "features_updated_at" FROM "public"."branches" "branches" INNER JOIN "public"."branch_schedules" "branch_schedules" ON "branch_schedules"."branch_id"="branches"."id" AND ("branch_schedules"."branch_id" = "branches"."id") LEFT JOIN "public"."branches_features" "branchFeature" ON "branchFeature"."branch_id"="branches"."id" AND ("branchFeature"."branch_id" = "branches"."id") LEFT JOIN "public"."features" "features" ON "features"."id"="branchFeature"."feature_id" ORDER BY branches.is_all_day DESC, branch_schedules.end_time::time DESC, "branch_schedules"."day" DESC, (case when "branch_schedules"."day" = 'holiday' then 1
when "branch_schedules"."day" = 'sunday' then 2
when "branch_schedules"."day" = 'saturday' then 3
when "branch_schedules"."day" = 'weekday' then 4
end) ASC, branch_schedules.start_time::time ASC LIMIT 5
But, through TypeORM, it retrieves a single result. If I take the generated SQL and execute it on the database, I get the 5 requested rows.
If I use .execute()
instead of getMany()
I get the 5 requested rows.
I have another (working) entity using the same queryBuilder
query, but without relations.
If this is not a bug and there is more to limit()
please link me to the correct part of the docs.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (1 by maintainers)
Top GitHub Comments
typeorm has a small tip in documentation about situation when you want to get limited amount of entities with many relations. They advice to use
.take(count)
instead of.limit(count)
. I had similar issue and it helped.using
.take()
is expensive, typeorm will fetch entity id first, then generating real query and map first query results ids toIN
query