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

Fix issue that caused certain uses of select to break #114

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
end

defp aggregate(query, repo, options) do
repo.aggregate(query, :count, options)
query
|> exclude(:select)
|> repo.aggregate(:count, options)
end

defp total_pages(0, _), do: 1
Expand Down
18 changes: 18 additions & 0 deletions test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page.total_pages == 1
end

test "pagination plays nice with distinct when selecting a map with a struct value" do
create_posts()

query =
from(post in Post,
distinct: post.id,
select: %{post: post}
)

page = Scrivener.Ecto.Repo.paginate(query)

assert length(page.entries) == 5
assert page.page_size == 5
assert page.page_number == 1
assert page.total_entries == 7
assert page.total_pages == 2
end

test "can specify prefix" do
create_users(6, "tenant_1")
create_users(2, "tenant_2")
Expand Down