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.

Strange behavior when using `limit` with relations (joins)

See original GitHub issue

Issue 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

29reactions
alitsvincommented, Sep 9, 2019

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.

11reactions
ardyfebcommented, Apr 23, 2020

using .take() is expensive, typeorm will fetch entity id first, then generating real query and map first query results ids to IN query

Read more comments on GitHub >

github_iconTop Results From Across the Web

Behavior of LIMIT using JOIN clause - Stack Overflow
In mysql when you have 3 tables (huge amount of data ) are joined by JOIN clause, at the end of the SELECT...
Read more >
Setting Healthy Limits--It Can Be An All-Win! - Mental Help Net
"Boundaries: Let someone else choose them and they're restrictions.Choose them yourself and they're principles."—Author Unknown.
Read more >
What's New in SQLAlchemy 1.0?
The behavior of joinedload.innerjoin as well as relationship.innerjoin is now to use “nested” inner joins, that is, right-nested, ...
Read more >
Chapter 4. Query Performance Optimization - O'Reilly
Many high-performance web sites use join decomposition. You can decompose a join by running multiple single-table queries instead of a ...
Read more >
Relationships & Social Skills - CHADD
Because ADHD is an “invisible disability,” often unrecognized by those who ... In relationships and marriages, the inappropriate social behavior may anger ......
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