Skip to content

Commit

Permalink
Exclude order_by when counting entries
Browse files Browse the repository at this point in the history
Previously when attempting to use a fragment in an order_by query, the
following error would be raised:

> ** (Postgrex.Error) ERROR 42703 (undefined_column): column
> "aliased_title" does not exist

The order of the results is not relevant when simply counting the
totals and has been excluded from the query.
  • Loading branch information
Gazler committed Feb 27, 2017
1 parent d6118c9 commit 899b7f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
query
|> exclude(:preload)
|> exclude(:select)
|> exclude(:order_by)
|> subquery
|> select(count("*"))
|> repo.one
Expand Down
12 changes: 12 additions & 0 deletions test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page.total_entries == 7
end

test "it handles complex order_by" do
create_posts()

page =
Post
|> select([p], fragment("? as aliased_title", p.title))
|> order_by([p], fragment("aliased_title"))
|> Scrivener.Ecto.Repo.paginate

assert page.total_entries == 7
end

test "can be provided the current page and page size as a params map" do
posts = create_posts()

Expand Down

0 comments on commit 899b7f0

Please sign in to comment.