Repo and Ecto.Query giving me Protocol.UndefinedError at GET /post

Hello,

sorry if it’s a stupid question, but I didn’t find anything in the docs regarding my topic.

So is it possible to do anything like that:

    Post
    |> Repo.all()
    |> Repo.preload(:comments)
    |> Repo.preload(:users)
    |> Ecto.Query.limit(10)
    |> offset(30)

I want to implement a pagination. Currently, I’m getting this error:

 Protocol.UndefinedError at GET /post
protocol Ecto.Queryable not implemented fo

Without the limit and offset the statement works as defined. Before looking for solutions to the problem, I want to make sure, if it is even possible what I’m trying to do or if I’m mixing something.

Thanks for any help!

There are two different kinds of data involved:

  • before the Repo.all, when there’s a query being built
  • after the Repo.all, when there’s a list of results

You want to move operations like limit and offset before sending the query with Repo.all:

    Post
    |> Ecto.Query.limit(10)
    |> offset(30)
    |> Repo.all()
    |> Repo.preload(:comments)
    |> Repo.preload(:users)
4 Likes

Ok that makes sense… :slight_smile:
Kind of obvious now :smiley:

Thank you!