cannot use ^xxx outside of match clauses
Asked Answered
E

4

41

This function:

defp entries(query, page_number, page_size) do
  offset = page_size * (page_number - 1)

  query
  |> limit([_], ^page_size) # error
  |> offset([_], ^offset)
  |> Repo.all
end

gives an exception:

cannot use ^pg_size outside of match clauses

Why is that and how to fix it?

Egger answered 9/8, 2016 at 6:58 Comment(0)
W
117

This is most usually a sign that you haven't imported appropriate macros from Ecto.Query.

Warhol answered 9/8, 2016 at 7:14 Comment(3)
The ones that you attempt to use: limit and offset.Warhol
import Ecto.Query. I would strongly suggest going through elixir-lang.org/getting-started/alias-require-and-import.html and hexdocs.pm/ecto/Ecto.Query.html if you haven't already.Warhol
Is it possible to report root cause error for this problem?Stclair
L
0

Try this:

query
|> limit(page_size)
|> offset(offset)
|> Repo.all
Ladin answered 9/8, 2016 at 7:8 Comment(0)
B
0

Another possible cause for this error is misspelled words. In my case it was form instead of from.

Biconvex answered 27/9, 2019 at 11:45 Comment(0)
D
-1

You must use ^ (pin operator - https://hexdocs.pm/elixir/Kernel.SpecialForms.html)

query
|> limit(^page_size)
|> offset(^v_offset) # I don't know if offset var override offset function of Ecto
|> Repo.all
David answered 27/11, 2018 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.