@@ -30,7 +30,7 @@ if Code.ensure_loaded?(Ecto) do
30
30
nil -> false
31
31
_ ->
32
32
first_limit = first + 1
33
- has_more_records_query = remove_select ( from things in query , limit: ^ first_limit )
33
+ has_more_records_query = make_query_countable ( from things in query , limit: ^ first_limit )
34
34
has_more_records_query = from things in has_more_records_query , select: count ( things . id )
35
35
repo . one ( has_more_records_query ) > first
36
36
end
@@ -39,7 +39,7 @@ if Code.ensure_loaded?(Ecto) do
39
39
nil -> false
40
40
_ ->
41
41
last_limit = last + 1
42
- has_prev_records_query = remove_select ( from things in query , limit: ^ last_limit )
42
+ has_prev_records_query = make_query_countable ( from things in query , limit: ^ last_limit )
43
43
has_prev_records_query = from things in has_prev_records_query , select: count ( things . id )
44
44
repo . one ( has_prev_records_query ) > last
45
45
end
@@ -114,15 +114,27 @@ if Code.ensure_loaded?(Ecto) do
114
114
end
115
115
116
116
def connection_count ( repo , query ) do
117
- query = remove_select ( query )
117
+ query = make_query_countable ( query )
118
118
count_query = from things in query , select: count ( things . id )
119
119
repo . one ( count_query )
120
120
end
121
121
122
+ defp make_query_countable ( query ) do
123
+ query
124
+ |> remove_select
125
+ |> remove_order
126
+ end
127
+
122
128
# Remove select if it exists so that we avoid `only one select
123
129
# expression is allowed in query` Ecto exception
124
130
defp remove_select ( query ) do
125
- % { query | select: nil }
131
+ query |> Ecto.Query . exclude ( :select )
132
+ end
133
+
134
+ # Remove order by if it exists so that we avoid `field X in "order_by"
135
+ # does not exist in the model source in query`
136
+ defp remove_order ( query ) do
137
+ query |> Ecto.Query . exclude ( :order_by )
126
138
end
127
139
end
128
140
end
0 commit comments