Skip to content

Using "count()" on table with select_related gives the total of all rows instead of the count of the primary select #588

Closed
@erichaydel

Description

@erichaydel
Contributor

Describe the bug
This is mainly apparent in pagination, where you want the count of the primary objects. The count currently takes the count of all rows, which will include the joined rows. So if an object has many related objects, the count will be the number of related objects.

To Reproduce
Steps to reproduce the behavior:
I have added a test to check count in the "test_aggr" file on my forked branch, as well as some potential code to fix the issue.
I wasn't sure if you wanted to introduce a breaking change, or if the original functionality was necessary for something. I ended up changing the default, but adding a "distinct" keyword for those who depend on the legacy behavior.

Expected behavior
Count should return the count of the primary rows, not the total number of rows including left joins

Versions (please complete the following information):

  • Postgres 11.4
  • Python 3.8.11
  • ormar 0.10.22

Activity

added a commit that references this issue on Mar 9, 2022

Fix collerek#588 Bug in queryset count() method

ec58f32
erichaydel

erichaydel commented on Mar 9, 2022

@erichaydel
ContributorAuthor

Well my MR doesn't solve the problem because it fails on mysql. I'll work on getting that to work, but it's fairly hard for me to test on MYSQL.

EDIT: This is fixed

added a commit that references this issue on Mar 9, 2022

Fix collerek#588 Bug in queryset count() method

a7e78bb
added 2 commits that reference this issue on Mar 17, 2022

Fix collerek#588 documentation for distinct keyword to count method

231bc7c

Fix collerek#588 documentation for distinct keyword to count method

11e6f1e
added a commit that references this issue on Mar 28, 2022

Merge pull request #589 from erichaydel/588-fix-queryset-count-bug

4776d1e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @erichaydel

      Issue actions

        Using "count()" on table with `select_related` gives the total of all rows instead of the count of the primary select · Issue #588 · collerek/ormar