fastapi-pagination: `Total` returns more values than expected (not applying distinct on count)

Hi,

I have the tables Author and Books.

Author Book
Alice A
Alice B
Bob C

In my FastAPI endpoint, when paginating the select(Author).join(Books) I want to respond with a list that has 2 dicts:

  • Alice (with her two books)
  • Bob (with his one book)

It works great but the total is off. Instead of counting the unique Authors, it counts the total rows of the query.

Is there an option to fix that?

Perhaps relevant:

  • using FastAPI (+pydantic)
  • async PostgreSQL

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Hi all,

Sorry for the long response.

Unfortunately, we can’t fix this issue using distinct count cause it will break the way pagination works. You need to play around with the way you fetch relationships, it should help to solve this issue.