Ecto.Query.from
from
, go back to Ecto.Query module for more information.
Creates a query.
It can either be a keyword query or a query expression.
If it is a keyword query the first argument must be
either an in
expression, or a value that implements
the Ecto.Queryable
protocol. If the query needs a
reference to the data source in any other part of the
expression, then an in
must be used to create a reference
variable. The second argument should be a keyword query
where the keys are expression types and the values are
expressions.
If it is a query expression the first argument must be
a value that implements the Ecto.Queryable
protocol
and the second argument the expression.
Keywords example
from(c in City, select: c)
Expressions example
City |> select([c], c)
Examples
def paginate(query, page, size) do
from query,
limit: ^size,
offset: ^((page-1) * size)
end
The example above does not use in
because limit
and offset
do not require a reference to the data source. However, extending
the query with a where expression would require the use of in
:
def published(query) do
from p in query, where: not(is_nil(p.published_at))
end
Notice we have created a p
variable to reference the query's
original data source. This assumes that the original query
only had one source. When the given query has more than one source,
positional or named bindings may be used to access the additional sources.
def published_multi(query) do
from [p,o] in query,
where: not(is_nil(p.published_at)) and not(is_nil(o.published_at))
end
Note that the variables p
and o
can be named whatever you like
as they have no importance in the query sent to the database.