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

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

    A convenient helper method for printing raw SQL

    def print_sql(queryable) do
      IO.inspect(Ecto.Adapters.SQL.to_sql(:all, Repo, queryable))
      queryable
    end
    
    def list_new_foos() do
      Foo
      |> where([foo], foo.bar == 1337)
      |> limit(100)
      |> print_sql
      |> Repo.all()
    end
    

提交回复
热议问题