Ecto.Query.API.struct
struct
, go back to Ecto.Query.API module for more information.
Used in select
to specify which struct fields should be returned.
For example, if you don't need all fields to be returned
as part of a struct, you can filter it to include only certain
fields by using struct/2
:
from p in Post,
select: struct(p, [:title, :body])
struct/2
can also be used to dynamically select fields:
fields = [:title, :body]
from p in Post, select: struct(p, ^fields)
As a convenience, select
allows developers to take fields
without an explicit call to struct/2
:
from p in Post, select: [:title, :body]
Or even dynamically:
fields = [:title, :body]
from p in Post, select: ^fields
For preloads, the selected fields may be specified from the parent:
from(city in City, preload: :country,
select: struct(city, [:country_id, :name, country: [:id, :population]]))
If the same source is selected multiple times with a struct
,
the fields are merged in order to avoid fetching multiple copies
from the database. In other words, the expression below:
from(city in City, preload: :country,
select: {struct(city, [:country_id]), struct(city, [:name])})
is expanded to:
from(city in City, preload: :country,
select: {struct(city, [:country_id, :name]), struct(city, [:country_id, :name])})
IMPORTANT: When filtering fields for associations, you MUST include the foreign keys used in the relationship, otherwise Ecto will be unable to find associated records.