fastapi-pagination: `total` returning wrong number of values

I use FastAPI and SQLAlchemy for my project. I have an endpoint that requires a lot of joins with a WHERE:

@router.get(
    "/{entity_id}/related_entities",
    status_code=status.HTTP_200_OK,
    response_model=Page[EntityWithEvents],
    description="Get all related entities for a specific entity",
)
async def get_specific_entity_related_entities(
    entity_id: int,
    db: Annotated[AsyncSession, Depends(get_db)],
) -> AbstractPage:
    return await paginate(
        db,
        select(Entity)
        .where(Entity.id == entity_id)
        .options(
            selectinload(Entity.related_entities).joinedload(RelatedEntity.category),
            selectinload(Entity.related_entities).joinedload(RelatedEntity.type),
            selectinload(Entity.related_entities).joinedload(RelatedEntity.language),
            selectinload(Entity.related_entities).joinedload(RelatedEntity.account),
        )
    )

The total result should be ~48 in my case. However, since I’m filtering on a specific entity_id, fastapi_pagination returns a total of 1, a single page despite a size of 20:

image

I should point out that the result of the main query is fine. Is there a solution to my problem?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

@uriyyo @moumoutte I ended up doing things differently, changing the pydantic scheme to do as you advised. So I paginated RelatedEntity.

I hope I will be able to do it this weekend. Sorry, I had pretty busy week 😢