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?

Nathan Long
  • 122,748
  • 97
  • 336
  • 451
Ramano
  • 605
  • 1
  • 6
  • 11

4 Answers4

117

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

michalmuskala
  • 11,028
  • 2
  • 36
  • 47
0

Try this:

query
|> limit(page_size)
|> offset(offset)
|> Repo.all
NoDisplayName
  • 15,246
  • 12
  • 62
  • 98
0

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

pdamoc
  • 2,798
  • 15
  • 19
-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
Gilvan Souza
  • 89
  • 1
  • 1