How can I see the raw SQL generated for an Ecto.Query?

前端 未结 4 1152
庸人自扰
庸人自扰 2021-02-01 15:02

I have an Ecto.Query and a Repo, such that I can call Repo.all(query) and get results. However, the results are not what I expect.

4条回答
  •  没有蜡笔的小新
    2021-02-01 15:35

    to_sql/2 is added to the module you use Ecto.Repo on. By convention, that module would be named MyApp.Repo (MyApp would be your app's name). Internally, it would use Ecto.Adapters.SQL.to_sql/3, but Ecto.Adapters.SQL is more of an internal module.

    Using it would look like:

    iex> query = Ecto.Query.where(Post, [p], p.views > 10)
    iex> MyApp.Repo.to_sql(:all, query)
    {"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p WHERE p.views > $1", [10]}
    

提交回复
热议问题