ho un Ecto.Query
e a Repo
, tale che posso chiamare Repo.all(query)
e ottieni risultati. Tuttavia, i risultati non sono quello che mi aspetto.
Come posso vedere l'SQL raw il Repo
genererà dal Ecto.Query
?
risposte:
29 per risposta № 1Puoi usare Ecto.Adapters.SQL.to_sql / 3:
iex> Ecto.Adapters.SQL.to_sql(:all, Repo, Post)
{"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []}
La query può essere qualsiasi struttura che implementa il Ecto.Queryable protocollo come Post
sopra (che è un modulo che importa Ecto.Schema
). Un Ecto.Query
può anche essere passato:
iex> query = Ecto.Query.where(Post, [p], p.views > 10)
iex> Ecto.Adapters.SQL.to_sql(:all, Repo, query)
{"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p WHERE p.views > $1", [10]}
2 per risposta № 2
Un comodo metodo di supporto per la stampa di SQL raw
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