At the beginning of this chapter, we ran a SQL query with Repo.query/3
as soon as we had a database connection, to verify that our connection was working as expected. Ultimately, Ecto is all about flexibility, so it lets you work with raw SQL as easily as you work with its query API.
Note
We need to import the Ecto.Query
module so we can write Ecto queries without the need to always prefix the needed macros (for example, where/3
and group_by/3
).
If you're used to SQL, you will feel right at home with Ecto queries. Ecto uses the same SQL keywords (from
, where
, select
, and so on) with the semantics you expect, so the following query, where we select a User
with a specific username
, won't catch you off-guard:
iex> query = from u in User, ...> where: u.username == "andre", ...> select: u #Ecto.Query<from u in ElixirDrip.Accounts.User, where: u.username == "andre", select: u> iex> Repo.one(query) 08:23:09.189 [debug] QUERY OK source="users" db=1.8ms queue=0.1ms SELECT...