#`list_and_count` service method not respecting `uniquify=True`

1 messages · Page 1 of 1 (latest)

final sonnet
#

This is what I am trying to do

items, total = tag_service.list_and_count(
  TagLibraryFilter(libraries=[library.id]),
  uniquify=True,
))

With my custom filter:

@dataclass
class TagLibraryFilter(StatementFilter):
    """Filter tags by library_id - returns tags with at least one book in the library."""

    libraries: Optional[list[int]] = None

    def append_to_statement(
        self, statement: StatementTypeT, model: type[ModelT], *args, **kwargs
    ) -> StatementTypeT:
        if self.libraries:
            statement = statement.where(
                m.BookTagLink.tag_id == m.Tag.id,
                m.BookTagLink.book.has(m.Book.library_id.in_(self.libraries)),
            )
        return super().append_to_statement(statement, model, *args, **kwargs)

Is this an error on my end? Here is the generated SQL, it seems to be counting all the rows, not only unique rows:

SELECT tags.name, tags.id, count(*) OVER () AS anon_1 
FROM tags, book_tag_link 
WHERE book_tag_link.tag_id = tags.id AND (EXISTS (SELECT 1 
FROM books 
WHERE books.id = book_tag_link.book_id AND books.library_id IN ($1::BIGINT)))
2025-11-14 17:47:56,833 INFO sqlalchemy.engine.Engine [cached since 0.1928s ago] (2,)
final sonnet
#

Also, pagination is affected in both list and list_and_count - the page size counts non-distinct rows, causing pages to contain fewer items than expected.