Pleroma v1.1.9-10-g42f76306+dev Pleroma.Repo View Source
Link to this section Summary
Functions
Calculate the given aggregate
over the given field
.
Fetches all entries from the data store matching the given query.
Checks out a connection for the duration of the function.
Returns the adapter configuration stored in the :otp_app
environment.
Deletes a struct using its primary key.
Same as c:delete/2
but returns the struct or raises if the changeset is invalid.
Deletes all entries matching the given query.
Checks if there exists an entry that matches the given query.
find resource based on prepared query
Fetches a single struct from the data store where the primary key matches the given id.
Similar to c:get/3
but raises Ecto.NoResultsError
if no record was found.
Gets association from cache or loads if need
Fetches a single result from the query.
Similar to c:get_by/3
but raises Ecto.NoResultsError
if no record was found.
Returns the atom name or pid of the current repository.
Returns true if the current process is inside a transaction.
Dynamically loads the repository url from the DATABASE_URL environment variable.
Inserts a struct defined via Ecto.Schema
or a changeset.
Same as c:insert/2
but returns the struct or raises if the changeset is invalid.
Inserts all entries into the repository.
Inserts or updates a changeset depending on whether the struct is persisted or not.
Same as c:insert_or_update/2
but returns the struct or raises if the changeset
is invalid.
Loads data
into a struct or a map.
Fetches a single result from the query.
Similar to c:one/2
but raises Ecto.NoResultsError
if no record was found.
Preloads all associations on the given struct or structs.
Sets the dynamic repository to be used in further iteractions.
A convenience function for SQL-based repositories that executes the given query.
A convenience function for SQL-based repositories that executes the given query.
Rolls back the current transaction.
Starts any connection pooling or supervision and return {:ok, pid}
or just :ok
if nothing needs to be done.
Shuts down the repository.
Returns a lazy enumerable that emits all entries from the data store matching the given query.
A convenience function for SQL-based repositories that translates the given query to SQL.
Runs the given function or Ecto.Multi
inside a transaction.
Updates a changeset using its primary key.
Same as c:update/2
but returns the struct or raises if the changeset is invalid.
Updates all entries matching the given query with the given values.
Link to this section Functions
Calculate the given aggregate
over the given field
.
If the query has a limit, offset or distinct set, it will be automatically wrapped in a subquery in order to return the proper result.
Any preload or select in the query will be ignored in favor of the column being aggregated.
The aggregation will fail if any group_by
field is set.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Examples
# Returns the number of visits per blog post
Repo.aggregate(Post, :count, :visits)
# Returns the number of visits per blog post in the "private" schema path
# (in Postgres) or database (in MySQL)
Repo.aggregate(Post, :count, :visits, prefix: "private")
# Returns the average number of visits for the top 10
query = from Post, limit: 10
Repo.aggregate(query, :avg, :visits)
Callback implementation for Ecto.Repo.aggregate/4
.
Fetches all entries from the data store matching the given query.
May raise Ecto.QueryError
if query validation fails.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Example
# Fetch all post titles
query = from p in Post,
select: p.title
MyRepo.all(query)
Callback implementation for Ecto.Repo.all/2
.
Checks out a connection for the duration of the function.
It returns the result of the function. This is useful when you need to perform multiple operations against the repository in a row and you want to avoid checking out the connection multiple times.
checkout/2
and transaction/2
can be combined and nested
multiple times. If checkout/2
is called inside the function
of another checkout/2
call, the function is simply executed,
without checking out a new connection.
Options
See the "Shared options" section at the module documentation.
Callback implementation for Ecto.Repo.checkout/2
.
Returns the adapter configuration stored in the :otp_app
environment.
If the c:init/2
callback is implemented in the repository,
it will be invoked with the first argument set to :runtime
.
Callback implementation for Ecto.Repo.config/0
.
Deletes a struct using its primary key.
If the struct has no primary key, Ecto.NoPrimaryKeyFieldError
will be raised. If the struct has been removed from db prior to
call, Ecto.StaleEntryError
will be raised.
It returns {:ok, struct}
if the struct has been successfully
deleted or {:error, changeset}
if there was a validation
or a known constraint error.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set in the schema.:stale_error_field
- The field where stale errors will be added in the returning changeset. This option can be used to avoid raisingEcto.StaleEntryError
.:stale_error_message
- The message to add to the configured:stale_error_field
when stale errors happen, defaults to "is stale".
See the "Shared options" section at the module documentation.
Example
post = MyRepo.get!(Post, 42)
case MyRepo.delete post do
{:ok, struct} -> # Deleted with success
{:error, changeset} -> # Something went wrong
end
Callback implementation for Ecto.Repo.delete/2
.
Same as c:delete/2
but returns the struct or raises if the changeset is invalid.
Callback implementation for Ecto.Repo.delete!/2
.
Deletes all entries matching the given query.
It returns a tuple containing the number of entries and any returned
result as second element. The second element is nil
by default
unless a select
is supplied in the update query. Note, however,
not all databases support returning data from DELETEs.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set in the schema.
See the "Shared options" section at the module documentation for remaining options.
Examples
MyRepo.delete_all(Post)
from(p in Post, where: p.id < 10) |> MyRepo.delete_all
Callback implementation for Ecto.Repo.delete_all/2
.
Checks if there exists an entry that matches the given query.
Returns a boolean.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Examples
# checks if any posts exist
Repo.exists?(Post)
# checks if any posts exist in the "private" schema path (in Postgres) or
# database (in MySQL)
Repo.exists?(Post, schema: "private")
# checks if any post with a like count greater than 10 exists
query = from p in Post, where: p.like_count > 10
Repo.exists?(query)
Callback implementation for Ecto.Repo.exists?/2
.
find_resource(query)
View Sourcefind_resource(Ecto.Query.t()) :: {:ok, struct()} | {:error, :not_found}
find resource based on prepared query
Fetches a single struct from the data store where the primary key matches the given id.
Returns nil
if no result was found. If the struct in the queryable
has no or more than one primary key, it will raise an argument error.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Example
MyRepo.get(Post, 42)
MyRepo.get(Post, 42, prefix: "public")
Callback implementation for Ecto.Repo.get/3
.
Similar to c:get/3
but raises Ecto.NoResultsError
if no record was found.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Example
MyRepo.get!(Post, 42)
MyRepo.get!(Post, 42, prefix: "public")
Callback implementation for Ecto.Repo.get!/3
.
Gets association from cache or loads if need
Examples
iex> Repo.get_assoc(token, :user) %User{}
Fetches a single result from the query.
Returns nil
if no result was found. Raises if more than one entry.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Example
MyRepo.get_by(Post, title: "My post")
MyRepo.get_by(Post, [title: "My post"], prefix: "public")
Callback implementation for Ecto.Repo.get_by/3
.
Similar to c:get_by/3
but raises Ecto.NoResultsError
if no record was found.
Raises if more than one entry.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Example
MyRepo.get_by!(Post, title: "My post")
MyRepo.get_by!(Post, [title: "My post"], prefix: "public")
Callback implementation for Ecto.Repo.get_by!/3
.
Returns the atom name or pid of the current repository.
See c:put_dynamic_repo/1
for more information.
Callback implementation for Ecto.Repo.get_dynamic_repo/0
.
Returns true if the current process is inside a transaction.
If you are using the Ecto.Adapters.SQL.Sandbox
in tests, note that even
though each test is inside a transaction, in_transaction?/0
will only
return true inside transactions explicitly created with transaction/2
. This
is done so the test environment mimics dev and prod.
If you are trying to debug transaction-related code while using
Ecto.Adapters.SQL.Sandbox
, it may be more helpful to configure the database
to log all statements and consult those logs.
Examples
MyRepo.in_transaction?
#=> false
MyRepo.transaction(fn ->
MyRepo.in_transaction? #=> true
end)
Callback implementation for Ecto.Repo.in_transaction?/0
.
Dynamically loads the repository url from the DATABASE_URL environment variable.
Inserts a struct defined via Ecto.Schema
or a changeset.
In case a struct is given, the struct is converted into a changeset with all non-nil fields as part of the changeset.
In case a changeset is given, the changes in the changeset are merged with the struct fields, and all of them are sent to the database.
It returns {:ok, struct}
if the struct has been successfully
inserted or {:error, changeset}
if there was a validation
or a known constraint error.
Options
:returning
- selects which fields to return. Whentrue
, returns all fields in the given struct. May be a list of fields, where a struct is still returned but only with the given fields. In any case, it will include fields withread_after_writes
set to true. Not all databases support this option.:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set any schemas. Also, the@schema_prefix
for the parent record will override all default@schema_prefix
s set in any child schemas for associations.:on_conflict
- It may be one of:raise
(the default),:nothing
,:replace_all
,:replace_all_except_primary_key
,{:replace, fields}
, a keyword list of update instructions or anEcto.Query
query for updates. See the "Upserts" section for more information.:conflict_target
- A list of column names to verify for conflicts. It is expected those columns to have unique indexes on them that may conflict. If none is specified, the conflict target is left up to the database. May also be{:constraint, constraint_name_as_atom}
in databases that support the "ON CONSTRAINT" expression, such as PostgreSQL.:stale_error_field
- The field where stale errors will be added in the returning changeset. This option can be used to avoid raisingEcto.StaleEntryError
.:stale_error_message
- The message to add to the configured:stale_error_field
when stale errors happen, defaults to "is stale".
See the "Shared options" section at the module documentation.
Examples
A typical example is calling MyRepo.insert/1
with a struct
and acting on the return value:
case MyRepo.insert %Post{title: "Ecto is great"} do
{:ok, struct} -> # Inserted with success
{:error, changeset} -> # Something went wrong
end
Upserts
c:insert/2
provides upserts (update or inserts) via the :on_conflict
option. The :on_conflict
option supports the following values:
:raise
- raises if there is a conflicting primary key or unique index:nothing
- ignores the error in case of conflicts:replace_all
- replace all values on the existing row with the values in the schema/changeset, including autogenerated fields such asinserted_at
andupdated_at
:replace_all_except_primary_key
- same as above except primary keys are not replaced{:replace, fields}
- replace only specific columns. This option requires conflict_target- a keyword list of update instructions - such as the one given to
c:update_all/3
, for example:[set: [title: "new title"]]
- an
Ecto.Query
that will act as anUPDATE
statement, such as the one given toc:update_all/3
. If the struct cannot be found,Ecto.StaleEntryError
will be raised.
Upserts map to "ON CONFLICT" on databases like Postgres and "ON DUPLICATE KEY" on databases such as MySQL.
As an example, imagine :title
is marked as a unique column in
the database:
{:ok, inserted} = MyRepo.insert(%Post{title: "this is unique"})
Now we can insert with the same title but do nothing on conflicts:
{:ok, ignored} = MyRepo.insert(%Post{title: "this is unique"}, on_conflict: :nothing)
assert ignored.id == nil
Because we used on_conflict: :nothing
, instead of getting an error,
we got {:ok, struct}
. However the returned struct does not reflect
the data in the database. One possible mechanism to detect if an
insert or nothing happened in case of on_conflict: :nothing
is by
checking the id
field. id
will be nil if the field is autogenerated
by the database and no insert happened.
For actual upserts, where an insert or update may happen, the situation is slightly more complex, as the database does not actually inform us if an insert or update happened. Let's insert a post with the same title but use a query to update the body column in case of conflicts:
# In Postgres (it requires the conflict target for updates):
on_conflict = [set: [body: "updated"]]
{:ok, updated} = MyRepo.insert(%Post{title: "this is unique"},
on_conflict: on_conflict, conflict_target: :title)
# In MySQL (conflict target is not supported):
on_conflict = [set: [title: "updated"]]
{:ok, updated} = MyRepo.insert(%Post{id: inserted.id, title: "updated"},
on_conflict: on_conflict)
In the examples above, even though it returned :ok
, we do not know
if we inserted new data or if we updated only the :on_conflict
fields.
In case an update happened, the data in the struct most likely does
not match the data in the database. For example, autogenerated fields
such as inserted_at
will point to now rather than the time the
struct was actually inserted.
If you need to guarantee the data in the returned struct mirrors the database, you have three options:
Use
on_conflict: :replace_all
, although that will replace all fields in the database with the ones in the struct/changeset, including autogenerated fields such asinsert_at
andupdated_at
:MyRepo.insert(%Post{title: "this is unique"}, on_conflict: :replace_all, conflict_target: :title)
Specify
read_after_writes: true
in your schema for choosing fields that are read from the database after every operation. Or passreturning: true
toinsert
to read all fields back:MyRepo.insert(%Post{title: "this is unique"}, returning: true, on_conflict: on_conflict, conflict_target: :title)
Alternatively, read the data again from the database in a separate query. This option requires the primary key to be generated by the database:
{:ok, updated} = MyRepo.insert(%Post{title: "this is unique"}, on_conflict: on_conflict) Repo.get(Post, updated.id)
Because of the inability to know if the struct is up to date or not,
inserting a struct with associations and using the :on_conflict
option
at the same time is not recommended, as Ecto will be unable to actually
track the proper status of the association.
Callback implementation for Ecto.Repo.insert/2
.
Same as c:insert/2
but returns the struct or raises if the changeset is invalid.
Callback implementation for Ecto.Repo.insert!/2
.
Inserts all entries into the repository.
It expects a schema module (MyApp.User
) or a source ("users"
) or
both ({"users", MyApp.User}
) as the first argument. The second
argument is a list of entries to be inserted, either as keyword
lists or as maps. The keys of the entries are the field names as
atoms and the value should be the respective value for the field
type or, optionally, an Ecto.Query
that returns a single entry
with a single value.
It returns a tuple containing the number of entries
and any returned result as second element. If the database
does not support RETURNING in INSERT statements or no
return result was selected, the second element will be nil
.
When a schema module is given, the entries given will be properly dumped
before being sent to the database. If the schema contains an
autogenerated ID field, it will be handled either at the adapter
or the storage layer. However any other autogenerated value, like
timestamps, won't be autogenerated when using c:insert_all/3
.
This is by design as this function aims to be a more direct way
to insert data into the database without the conveniences of
c:insert/2
. This is also consistent with c:update_all/3
that
does not handle timestamps as well.
It is also not possible to use insert_all
to insert across multiple
tables, therefore associations are not supported.
If a source is given, without a schema module, the given fields are passed as is to the adapter.
Options
:returning
- selects which fields to return. Whentrue
, returns all fields in the given schema. May be a list of fields, where a struct is still returned but only with the given fields. Orfalse
, where nothing is returned (the default). This option is not supported by all databases.:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set in the schema.:on_conflict
- It may be one of:raise
(the default),:nothing
,:replace_all
,:replace_all_except_primary_key
,{:replace, fields}
, a keyword list of update instructions or anEcto.Query
query for updates. See the "Upserts" section for more information.:conflict_target
- A list of column names to verify for conflicts. It is expected those columns to have unique indexes on them that may conflict. If none is specified, the conflict target is left up to the database. It may also be{:constraint, constraint_name_as_atom}
in databases that support the "ON CONSTRAINT" expression, such as PostgreSQL, or{:unsafe_fragment, binary_fragment}
to pass any expression to the database without any sanitization, such asON CONFLICT (coalesce(firstname, ""), coalesce(lastname, ""))
.
See the "Shared options" section at the module documentation for remaining options.
Examples
MyRepo.insert_all(Post, [[title: "My first post"], [title: "My second post"]])
MyRepo.insert_all(Post, [%{title: "My first post"}, %{title: "My second post"}])
Upserts
c:insert_all/3
provides upserts (update or inserts) via the :on_conflict
option. The :on_conflict
option supports the following values:
:raise
- raises if there is a conflicting primary key or unique index:nothing
- ignores the error in case of conflicts:replace_all
- replace all values on the existing row by the new entry, including values not sent explicitly by Ecto, such as database defaults. This option requires a schema:replace_all_except_primary_key
- same as above except primary keys are not replaced. This option requires a schema{:replace, fields}
- replace only specific columns. This option requires conflict_target- a keyword list of update instructions - such as the one given to
c:update_all/3
, for example:[set: [title: "new title"]]
- an
Ecto.Query
that will act as anUPDATE
statement, such as the one given toc:update_all/3
Upserts map to "ON CONFLICT" on databases like Postgres and "ON DUPLICATE KEY" on databases such as MySQL.
Return values
By default, both Postgres and MySQL return the amount of entries
inserted on c:insert_all/3
. However, when the :on_conflict
option
is specified, Postgres will only return a row if it was affected
while MySQL returns at least the number of entries attempted.
For example, if :on_conflict
is set to :nothing
, Postgres will
return 0 if no new entry was added while MySQL will still return
the amount of entries attempted to be inserted, even if no entry
was added. Even worse, if :on_conflict
is query, MySQL will return
the number of attempted entries plus the number of entries modified
by the UPDATE query.
Callback implementation for Ecto.Repo.insert_all/3
.
Inserts or updates a changeset depending on whether the struct is persisted or not.
The distinction whether to insert or update will be made on the
Ecto.Schema.Metadata
field :state
. The :state
is automatically set by
Ecto when loading or building a schema.
Please note that for this to work, you will have to load existing structs from the database. So even if the struct exists, this won't work:
struct = %Post{id: "existing_id", ...}
MyRepo.insert_or_update changeset
# => {:error, changeset} # id already exists
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set any schemas. Also, the@schema_prefix
for the parent record will override all default@schema_prefix
s set in any child schemas for associations.:stale_error_field
- The field where stale errors will be added in the returning changeset. This option can be used to avoid raisingEcto.StaleEntryError
. Only applies to updates.:stale_error_message
- The message to add to the configured:stale_error_field
when stale errors happen, defaults to "is stale". Only applies to updates.
See the "Shared options" section at the module documentation.
Example
result =
case MyRepo.get(Post, id) do
nil -> %Post{id: id} # Post not found, we build one
post -> post # Post exists, let's use it
end
|> Post.changeset(changes)
|> MyRepo.insert_or_update
case result do
{:ok, struct} -> # Inserted or updated with success
{:error, changeset} -> # Something went wrong
end
Callback implementation for Ecto.Repo.insert_or_update/2
.
Same as c:insert_or_update/2
but returns the struct or raises if the changeset
is invalid.
Callback implementation for Ecto.Repo.insert_or_update!/2
.
Loads data
into a struct or a map.
The first argument can be a a schema module, or a map (of types) and determines the return value: a struct or a map, respectively.
The second argument data
specifies fields and values that are to be loaded.
It can be a map, a keyword list, or a {fields, values}
tuple.
Fields can be atoms or strings.
Fields that are not present in the schema (or types
map) are ignored.
If any of the values has invalid type, an error is raised.
Examples
iex> MyRepo.load(User, %{name: "Alice", age: 25})
%User{name: "Alice", age: 25}
iex> MyRepo.load(User, [name: "Alice", age: 25])
%User{name: "Alice", age: 25}
data
can also take form of {fields, values}
:
iex> MyRepo.load(User, {[:name, :age], ["Alice", 25]})
%User{name: "Alice", age: 25, ...}
The first argument can also be a types
map:
iex> types = %{name: :string, age: :integer}
iex> MyRepo.load(types, %{name: "Alice", age: 25})
%{name: "Alice", age: 25}
This function is especially useful when parsing raw query results:
iex> result = Ecto.Adapters.SQL.query!(MyRepo, "SELECT * FROM users", [])
iex> Enum.map(result.rows, &MyRepo.load(User, {result.columns, &1}))
[%User{...}, ...]
Callback implementation for Ecto.Repo.load/2
.
Fetches a single result from the query.
Returns nil
if no result was found. Raises if more than one entry.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Examples
Repo.one(Post)
Repo.one(from p in Post, where: p.like_count > 10)
query = from p in Post, where: p.like_count > 10
Repo.one(query, prefix: "private")
Callback implementation for Ecto.Repo.one/2
.
Similar to c:one/2
but raises Ecto.NoResultsError
if no record was found.
Raises if more than one entry.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation.
Callback implementation for Ecto.Repo.one!/2
.
Preloads all associations on the given struct or structs.
This is similar to Ecto.Query.preload/3
except it allows
you to preload structs after they have been fetched from the
database.
In case the association was already loaded, preload won't attempt to reload it.
Options
Besides the "Shared options" section at the module documentation, it accepts:
:force
- By default, Ecto won't preload associations that are already loaded. By setting this option to true, any existing association will be discarded and reloaded.:in_parallel
- If the preloads must be done in parallel. It can only be performed when we have more than one preload and the repository is not in a transaction. Defaults totrue
.:prefix
- the prefix to fetch preloads from. By default, queries will use the same prefix as the one in the given collection. This option allows the prefix to be changed.
Examples
# Use a single atom to preload an association
posts = Repo.preload posts, :comments
# Use a list of atoms to preload multiple associations
posts = Repo.preload posts, [:comments, :authors]
# Use a keyword list to preload nested associations as well
posts = Repo.preload posts, [comments: [:replies, :likes], authors: []]
# Use a keyword list to customize how associations are queried
posts = Repo.preload posts, [comments: from(c in Comment, order_by: c.published_at)]
# Use a two-element tuple for a custom query and nested association definition
query = from c in Comment, order_by: c.published_at
posts = Repo.preload posts, [comments: {query, [:replies, :likes]}]
The query given to preload may also preload its own associations.
Callback implementation for Ecto.Repo.preload/3
.
Sets the dynamic repository to be used in further iteractions.
Sometimes, you may want a single Ecto repository to talk to
many different database instances. By default, when you call
MyApp.Repo.start_link/1
, it will start a repository with
name MyApp.Repo
. But if you want to start multiple repositories,
you can give each of them a different name:
MyApp.Repo.start_link(name: :tenant_foo, hostname: "foo.example.com")
MyApp.Repo.start_link(name: :tenant_bar, hostname: "bar.example.com")
You can also start repositories without names by explicitly setting the name to nil:
MyApp.Repo.start_link(name: nil, hostname: "temp.example.com")
However, once the repository is started, you can't directly interact with
it, since all operations in MyApp.Repo
are sent to the repository named
MyApp.Repo
. With put_dynamic_repo/1
, we can tell Ecto exactly which
instance to use:
MyApp.Repo.put_dynamic_repo(:tenant_foo)
Note this feature is experimental and may be changed or removed in future releases.
Callback implementation for Ecto.Repo.put_dynamic_repo/1
.
A convenience function for SQL-based repositories that executes the given query.
See Ecto.Adapters.SQL.query/4
for more information.
A convenience function for SQL-based repositories that executes the given query.
See Ecto.Adapters.SQL.query!/4
for more information.
Rolls back the current transaction.
The transaction will return the value given as {:error, value}
.
Callback implementation for Ecto.Repo.rollback/1
.
Starts any connection pooling or supervision and return {:ok, pid}
or just :ok
if nothing needs to be done.
Returns {:error, {:already_started, pid}}
if the repo is already
started or {:error, term}
in case anything else goes wrong.
Options
See the configuration in the moduledoc for options shared between adapters, for adapter-specific configuration see the adapter's documentation.
Callback implementation for Ecto.Repo.start_link/1
.
Shuts down the repository.
Callback implementation for Ecto.Repo.stop/1
.
Returns a lazy enumerable that emits all entries from the data store matching the given query.
SQL adapters, such as Postgres and MySQL, can only enumerate a stream inside a transaction.
May raise Ecto.QueryError
if query validation fails.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.:max_rows
- The number of rows to load from the database as we stream. It is supported at least by Postgres and MySQL and defaults to 500.
See the "Shared options" section at the module documentation.
Example
# Fetch all post titles
query = from p in Post,
select: p.title
stream = MyRepo.stream(query)
MyRepo.transaction(fn() ->
Enum.to_list(stream)
end)
Callback implementation for Ecto.Repo.stream/2
.
A convenience function for SQL-based repositories that translates the given query to SQL.
See Ecto.Adapters.SQL.to_sql/3
for more information.
Runs the given function or Ecto.Multi
inside a transaction.
Use with function
If an unhandled error occurs the transaction will be rolled back
and the error will bubble up from the transaction function.
If no error occurred the transaction will be committed when the
function returns. A transaction can be explicitly rolled back
by calling c:rollback/1
, this will immediately leave the function
and return the value given to rollback
as {:error, value}
.
A successful transaction returns the value returned by the function
wrapped in a tuple as {:ok, value}
.
If c:transaction/2
is called inside another transaction, the function
is simply executed, without wrapping the new transaction call in any
way. If there is an error in the inner transaction and the error is
rescued, or the inner transaction is rolled back, the whole outer
transaction is marked as tainted, guaranteeing nothing will be committed.
Use with Ecto.Multi
Besides functions transaction can be used with an Ecto.Multi struct.
Transaction will be started, all operations applied and in case of
success committed returning {:ok, changes}
. In case of any errors
the transaction will be rolled back and
{:error, failed_operation, failed_value, changes_so_far}
will be
returned.
You can read more about using transactions with Ecto.Multi
as well as
see some examples in the Ecto.Multi
documentation.
Options
See the "Shared options" section at the module documentation.
Examples
import Ecto.Changeset, only: [change: 2]
MyRepo.transaction(fn ->
MyRepo.update!(change(alice, balance: alice.balance - 10))
MyRepo.update!(change(bob, balance: bob.balance + 10))
end)
# Roll back a transaction explicitly
MyRepo.transaction(fn ->
p = MyRepo.insert!(%Post{})
if not Editor.post_allowed?(p) do
MyRepo.rollback(:posting_not_allowed)
end
end)
# With Ecto.Multi
Ecto.Multi.new
|> Ecto.Multi.insert(:post, %Post{})
|> MyRepo.transaction
Callback implementation for Ecto.Repo.transaction/2
.
Updates a changeset using its primary key.
A changeset is required as it is the only mechanism for
tracking dirty changes. Only the fields present in the changes
part
of the changeset are sent to the database. Any other, in-memory
changes done to the schema are ignored.
If the struct has no primary key, Ecto.NoPrimaryKeyFieldError
will be raised.
If the struct cannot be found, Ecto.StaleEntryError
will be raised.
It returns {:ok, struct}
if the struct has been successfully
updated or {:error, changeset}
if there was a validation
or a known constraint error.
Options
Besides the "Shared options" section at the module documentation, it accepts:
:force
- By default, if there are no changes in the changeset,c:update/2
is a no-op. By setting this option to true, update callbacks will always be executed, even if there are no changes (including timestamps).:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set any schemas. Also, the@schema_prefix
for the parent record will override all default@schema_prefix
s set in any child schemas for associations.:stale_error_field
- The field where stale errors will be added in the returning changeset. This option can be used to avoid raisingEcto.StaleEntryError
.:stale_error_message
- The message to add to the configured:stale_error_field
when stale errors happen, defaults to "is stale".
Example
post = MyRepo.get!(Post, 42)
post = Ecto.Changeset.change post, title: "New title"
case MyRepo.update post do
{:ok, struct} -> # Updated with success
{:error, changeset} -> # Something went wrong
end
Callback implementation for Ecto.Repo.update/2
.
Same as c:update/2
but returns the struct or raises if the changeset is invalid.
Callback implementation for Ecto.Repo.update!/2
.
Updates all entries matching the given query with the given values.
It returns a tuple containing the number of entries and any returned
result as second element. The second element is nil
by default
unless a select
is supplied in the update query. Note, however,
not all databases support returning data from UPDATEs.
Keep in mind this update_all
will not update autogenerated
fields like the updated_at
columns.
See Ecto.Query.update/3
for update operations that can be
performed on fields.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any@schema_prefix
set in the schema.
See the "Shared options" section at the module documentation for remaining options.
Examples
MyRepo.update_all(Post, set: [title: "New title"])
MyRepo.update_all(Post, inc: [visits: 1])
from(p in Post, where: p.id < 10, select: p.visits)
|> MyRepo.update_all(set: [title: "New title"])
from(p in Post, where: p.id < 10, update: [set: [title: "New title"]])
|> MyRepo.update_all([])
from(p in Post, where: p.id < 10, update: [set: [title: ^new_title]])
|> MyRepo.update_all([])
from(p in Post, where: p.id < 10, update: [set: [title: fragment("upper(?)", ^new_title)]])
|> MyRepo.update_all([])
Callback implementation for Ecto.Repo.update_all/3
.