Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting limit while using findAndCount() always returns one result even if more are available #2867

Closed
suavelizard opened this issue Mar 7, 2022 · 5 comments

Comments

@suavelizard
Copy link

suavelizard commented Mar 7, 2022

Describe the bug
Setting limit while using findAndCount always returns one result even if more are available.

To Reproduce
Steps to reproduce the behavior:

const [results, count]= await this.fooRepository.findAndCount(
      {
        $and: [{ organization: organization }, {}],
      },
      {
         limit: 5
      },
    );
console.log(results.length, count);

Output: 1 7

Generated SQL

select
    "r0" ."uuid",
    "r0" ."created_at",
    "r0" ."updated_at",
    "r0" ."deleted_at",
    "r0" ."slug",
    "r0" ."title",
    "r0" ."description",
    "r0" ."created_by_uuid",
    "r0" ."organization_uuid",
    "c1" ."uuid" as "c1__uuid",
    "c1" ."created_at" as "c1__created_at",
    "c1" ."updated_at" as "c1__updated_at",
    "c1" ."deleted_at" as "c1__deleted_at",
    "c1" ."body" as "c1__body",
    "c1" ."author_uuid" as "c1__author_uuid",
    "c1" ."target_uuid" as "c1__target_uuid",
    "s2" ."uuid" as "s2__uuid",
    "s2" ."created_at" as "s2__created_at",
    "s2" ."updated_at" as "s2__updated_at",
    "s2" ."deleted_at" as "s2__deleted_at",
    "s2" ."article_uuid" as "s2__article_uuid",
    "s2" ."order" as "s2__order",
    "s2" ."description" as "s2__description",
    "s2" ."title" as "s2__title"
from
    "article" as "r0"
    left join "comment" as "c1" on "r0" ."uuid" = "c1" ."target_uuid"
    left join "article_step" as "s2" on "r0" ."uuid" = "s2" ."article_uuid"
where
    "r0" ."organization_uuid" = '371245b2-e60d-401c-bcd0-b42860916a32'
limit
    5

Expected behavior
Limit should return up to 5 items if they are available not one.

Additional context
Removing limit returns all 8 results.

Versions

Dependency Version
node v16.13.1
typescript "typescript": "^4.3.5"
mikro-orm "@mikro-orm/core": "^5.0.3"
your-driver "@mikro-orm/postgresql": "^5.0.3"
@B4nan
Copy link
Member

B4nan commented Mar 7, 2022

Please always upgrade to latest before filing issues, and note that you are supposed to say what exact versions are you using - so not a range, an exact version.

This has been fixed in v5.0.5.

@B4nan B4nan closed this as completed Mar 7, 2022
@suavelizard
Copy link
Author

suavelizard commented Mar 8, 2022

I might be doing something wrong but I just upgraded to 5.0.5 and purged node_modules

select
    "r0" ."uuid",
    "r0" ."created_at",
    "r0" ."updated_at",
    "r0" ."deleted_at",
    "r0" ."slug",
    "r0" ."title",
    "r0" ."description",
    "r0" ."created_by_uuid",
    "r0" ."organization_uuid",
    "c1" ."uuid" as "c1__uuid",
    "c1" ."created_at" as "c1__created_at",
    "c1" ."updated_at" as "c1__updated_at",
    "c1" ."deleted_at" as "c1__deleted_at",
    "c1" ."body" as "c1__body",
    "c1" ."author_uuid" as "c1__author_uuid",
    "c1" ."target_uuid" as "c1__target_uuid",
    "s2" ."uuid" as "s2__uuid",
    "s2" ."created_at" as "s2__created_at",
    "s2" ."updated_at" as "s2__updated_at",
    "s2" ."deleted_at" as "s2__deleted_at",
    "s2" ."runbook_uuid" as "s2__runbook_uuid",
    "s2" ."order" as "s2__order",
    "s2" ."description" as "s2__description",
    "s2" ."title" as "s2__title"
from
    "runbook" as "r0"
    left join "comment" as "c1" on "r0" ."uuid" = "c1" ."target_uuid"
    left join "runbook_step" as "s2" on "r0" ."uuid" = "s2" ."runbook_uuid"
where
    "r0" ."organization_uuid" = '371245b2-e60d-401c-bcd0-b42860916a32'
limit
    10	

I can see this generated query returning 10 of the same row with the same uuid in postgres and one row via the ORM
The logged output of count is 7 and I see 7 rows matching in postgres when using

select
    "r0" ."uuid",
    "r0" ."created_at",
    "r0" ."updated_at",
    "r0" ."deleted_at",
    "r0" ."slug",
    "r0" ."title",
    "r0" ."description",
    "r0" ."created_by_uuid",
    "r0" ."organization_uuid"
from
    "runbook" as "r0"

where
    "r0" ."organization_uuid" = '371245b2-e60d-401c-bcd0-b42860916a32'
limit
    10

Versions

Dependency Version
node v16.13.1
typescript 4.6.2
mikro-orm 5.0.5
your-driver postgresql@5.0.5

@B4nan
Copy link
Member

B4nan commented Mar 8, 2022

Can you try this?

const [results, count]= await this.fooRepository.findAndCount(
      {
        $and: [{ organization: organization }, {}],
      },
      {
         limit: 5,
         flags: [QueryFlag.PAGINATE],
      },
    );
console.log(results.length, count);

The query needs to be a subquery, which is what the paginate flag does. It should be applied automatically in latest version, but apparently only when using QB directly.

@suavelizard
Copy link
Author

Yeah that works. Interesting. Thank you.

@B4nan
Copy link
Member

B4nan commented Mar 10, 2022

Ok let's reopen then, this should work automatically nowadays.

@B4nan B4nan reopened this Mar 10, 2022
@B4nan B4nan closed this as completed in ccb4223 Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants