Loading...
Loading...
Use when querying data with Ecto.Query DSL including where clauses, joins, aggregates, preloading, and query composition. Use for building flexible database queries in Elixir applications.
npx skill4agent add thebushidocollective/han ecto-query-patternsimport Ecto.Query, only: [from: 2]
# Basic query using keyword syntax
query = from u in "users",
where: u.age > 18,
select: u.name
# Execute the query
MyApp.Repo.all(query)from/2Repoall/1one/1get/2query = from u in MyApp.User,
where: u.age > 18,
select: u.name
MyApp.Repo.all(query)from MyApp.Post,
where: [category: "fresh and new"],
order_by: [desc: :published_at],
select: [:id, :title, :body]query = from p in MyApp.Post,
where: p.category == "fresh and new",
order_by: [desc: p.published_at],
select: struct(p, [:id, :title, :body])
MyApp.Repo.all(query)pstruct/2category = "fresh and new"
order_by = [desc: :published_at]
select_fields = [:id, :title, :body]
query = from MyApp.Post,
where: [category: ^category],
order_by: ^order_by,
select: ^select_fields
MyApp.Repo.all(query)^query = from u in MyApp.User,
where: u.age > 0,
select: u.name
# Multiple where clauses are combined with AND
query = from u in MyApp.User,
where: u.age > 18,
where: u.confirmed == true,
select: u
MyApp.Repo.all(query)where# Create a base query
query = from u in MyApp.User, where: u.age > 18
# Extend the query
query = from u in query, select: u.name
MyApp.Repo.all(query)indef most_recent_from(query, minimum_date) do
from p in query,
where: p.published_at > ^minimum_date,
order_by: [desc: p.published_at]
end
# Usage
MyApp.Post
|> most_recent_from(~N[2024-01-01 00:00:00])
|> MyApp.Repo.all()from p in MyApp.Post,
where: p.category == "elixir" or p.category == "phoenix",
select: porEcto.Query.dynamic/2categories = ["elixir", "phoenix", "ecto"]
query = from p in MyApp.Post,
where: p.category in ^categories,
select: p
MyApp.Repo.all(query)insearch_term = "%elixir%"
query = from p in MyApp.Post,
where: like(p.title, ^search_term),
select: p
# Case-insensitive version
query = from p in MyApp.Post,
where: ilike(p.title, ^search_term),
select: plike/2ilike/2%# Select multiple fields
query = from p in MyApp.Post,
select: {p.id, p.title}
MyApp.Repo.all(query) # Returns [{1, "Title 1"}, {2, "Title 2"}]
# Select as map
query = from p in MyApp.Post,
select: %{id: p.id, title: p.title}
MyApp.Repo.all(query) # Returns [%{id: 1, title: "Title 1"}, ...]
# Select struct with specific fields
query = from p in MyApp.Post,
select: struct(p, [:id, :title, :body])
MyApp.Repo.all(query) # Returns Post structs with only selected fields loaded# Count records
query = from p in MyApp.Post,
select: count(p.id)
MyApp.Repo.one(query) # Returns integer count
# Average
query = from p in MyApp.Post,
select: avg(p.rating)
# Sum
query = from o in MyApp.Order,
select: sum(o.total)
# Min and Max
query = from p in MyApp.Product,
select: {min(p.price), max(p.price)}count/1avg/1sum/1min/1max/1query = from p in MyApp.Post,
group_by: p.category,
select: {p.category, count(p.id)}
MyApp.Repo.all(query) # Returns [{"elixir", 10}, {"phoenix", 5}]
# With having clause
query = from p in MyApp.Post,
group_by: p.category,
having: count(p.id) > 5,
select: {p.category, count(p.id)}group_byhaving# Single field ascending
query = from p in MyApp.Post,
order_by: p.published_at
# Single field descending
query = from p in MyApp.Post,
order_by: [desc: p.published_at]
# Multiple fields
query = from p in MyApp.Post,
order_by: [desc: p.published_at, asc: p.title]
# With nulls positioning
query = from p in MyApp.Post,
order_by: [desc_nulls_last: p.published_at]order_by# Simple limit
query = from p in MyApp.Post,
limit: 10
# With offset for pagination
page = 2
per_page = 10
query = from p in MyApp.Post,
order_by: [desc: p.published_at],
limit: ^per_page,
offset: ^((page - 1) * per_page)
MyApp.Repo.all(query)limitoffsetorder_byquery = from p in MyApp.Post,
join: c in MyApp.Comment,
on: c.post_id == p.id,
select: {p.title, c.body}
MyApp.Repo.all(query)onquery = from p in MyApp.Post,
join: c in assoc(p, :comments),
select: {p, c}
MyApp.Repo.all(query)assoc/2query = from p in MyApp.Post,
left_join: c in assoc(p, :comments),
select: {p, c}
MyApp.Repo.all(query)# Preload in separate query
MyApp.Repo.all(from p in MyApp.Post, preload: [:comments])
# Preload multiple associations
MyApp.Repo.all(from p in MyApp.Post, preload: [:comments, :author])
# Nested preload
MyApp.Repo.all(from p in MyApp.Post, preload: [:author, comments: :likes])query = from p in MyApp.Post,
join: c in assoc(p, :comments),
where: c.published_at > p.updated_at,
preload: [comments: c]
MyApp.Repo.all(query)query = from p in MyApp.Post,
join: c in assoc(p, :comments),
join: l in assoc(c, :likes),
where: l.inserted_at > c.updated_at,
preload: [:author, comments: {c, likes: l}]
MyApp.Repo.all(query)posts = MyApp.Repo.all(MyApp.Post)
posts_with_comments = MyApp.Repo.preload(posts, :comments)
# Preload with custom query
comments_query = from c in MyApp.Comment, order_by: [desc: c.inserted_at]
posts_with_recent_comments = MyApp.Repo.preload(posts, comments: comments_query)preload/2# Define subquery
subquery = from p in MyApp.Post,
where: p.published == true,
select: %{category: p.category, count: count(p.id)},
group_by: p.category
# Use subquery
query = from s in subquery(subquery),
where: s.count > 10,
select: s.category
MyApp.Repo.all(query)# Use SQL fragment
query = from p in MyApp.Post,
where: fragment("lower(?)", p.title) == "elixir",
select: p
# Fragment with parameters
search = "elixir"
query = from p in MyApp.Post,
where: fragment("lower(?) LIKE ?", p.title, ^"%#{search}%"),
select: pfragment/1query = from p in MyApp.Post,
hints: ["USE INDEX FOO"],
where: p.title == "title"
# Multiple hints
query = from p in MyApp.Post,
hints: "TABLESAMPLE SYSTEM(1)"
# Dynamic hints
sample = "SYSTEM_ROWS(1)"
query = from p in MyApp.Post,
hints: ["TABLESAMPLE", unsafe_fragment(^sample)]defmodule MyApp.PostQueries do
import Ecto.Query
def filter(query \\ MyApp.Post, filters) do
query
|> filter_by_category(filters[:category])
|> filter_by_published(filters[:published])
|> filter_by_search(filters[:search])
end
defp filter_by_category(query, nil), do: query
defp filter_by_category(query, category) do
from p in query, where: p.category == ^category
end
defp filter_by_published(query, nil), do: query
defp filter_by_published(query, published) do
from p in query, where: p.published == ^published
end
defp filter_by_search(query, nil), do: query
defp filter_by_search(query, search) do
from p in query, where: ilike(p.title, ^"%#{search}%")
end
end
# Usage
filters = %{category: "elixir", published: true, search: "ecto"}
MyApp.PostQueries.filter(filters) |> MyApp.Repo.all()defmodule MyApp.PostQueries do
import Ecto.Query
def search(filters) do
MyApp.Post
|> where(^build_where_clause(filters))
|> MyApp.Repo.all()
end
defp build_where_clause(filters) do
Enum.reduce(filters, dynamic(true), fn
{:category, value}, dynamic ->
dynamic([p], ^dynamic and p.category == ^value)
{:published, value}, dynamic ->
dynamic([p], ^dynamic and p.published == ^value)
{:min_rating, value}, dynamic ->
dynamic([p], ^dynamic and p.rating >= ^value)
_, dynamic ->
dynamic
end)
end
enddynamic/2# Distinct on all selected fields
query = from p in MyApp.Post,
distinct: true,
select: p.category
# Distinct on specific fields
query = from p in MyApp.Post,
distinct: [desc: p.published_at],
select: pdistinctposts_query = from p in MyApp.Post,
where: p.published == true,
select: %{type: "post", title: p.title}
pages_query = from p in MyApp.Page,
where: p.active == true,
select: %{type: "page", title: p.title}
# Union
query = posts_query |> union(^pages_query)
MyApp.Repo.all(query)
# Union all (includes duplicates)
query = posts_query |> union_all(^pages_query)union/2union_all/2# Pessimistic locking
query = from p in MyApp.Post,
where: p.id == ^post_id,
lock: "FOR UPDATE"
post = MyApp.Repo.one(query)
# Optimistic locking (using version field in schema)
changeset = MyApp.Post.changeset(post, params)
case MyApp.Repo.update(changeset) do
{:ok, updated_post} -> # Success
{:error, changeset} -> # Failed, possibly due to concurrent update
enddefp newest_records(parent_ids, assoc, n) do
%{related_key: related_key, queryable: queryable} = assoc
squery = from q in queryable,
where: field(q, ^related_key) == parent_as(:parent_ids).id,
order_by: {:desc, :created_at},
limit: ^n
query = from f in fragment("SELECT id from UNNEST(?::int[]) AS id", ^parent_ids),
as: :parent_ids,
inner_lateral_join: s in subquery(squery),
on: true,
select: s
MyApp.Repo.all(query)
endquery = from p in MyApp.Post, as: :posts
query = from [posts: p] in query,
join: c in assoc(p, :comments), as: :comments
query = from [posts: p, comments: c] in query,
where: c.score > 10,
select: {p.title, c.body}^preloadorder_bylimitoffsetassoc/2Ecto.Query.dynamic/2^Repo.all/1order_byRepo.preload/2