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

前端 未结 4 1079
庸人自扰
庸人自扰 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:50

    It's basically Gazlers answer, but modified to use in code:

    query = from p in Post
    {query, params} = Ecto.Adapters.SQL.to_sql(:all, Repo, query)
    IO.puts("#{query}, #{inspect(params)}")
    

    You could use simple IO.inspect, but it'll output a query with backslashes.

提交回复
热议问题