14
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ectoを使った実装をしている際、生成されるSQL文を確認したいときに使っています。

Ecto.Adapters.SQL.to_sql/3を使う書き方もありますが、

iex> import Ecto.Query
iex> alias NaiveTree.Repo
iex> alias Categories.Category
iex> query = from c in Category
iex> Ecto.Adapters.SQL.to_sql(:all, Repo, query)
{"SELECT c0.\"id\", c0.\"name\", c0.\"parent_id\", c0.\"inserted_at\", c0.\"updated_at\" FROM \"categories\" AS c0",
 []}

to_sqlはRepo配下でも実行できます。こっちのほうが楽かも。

iex> Repo.to_sql(:all, query)
{"SELECT c0.\"id\", c0.\"name\", c0.\"parent_id\", c0.\"inserted_at\", c0.\"updated_at\" FROM \"categories\" AS c0",
 []}
14
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?