Ecto.Repo.update
You're seeing just the callback
update
, go back to Ecto.Repo module for more information.
Specs
update(changeset :: Ecto.Changeset.t(), opts :: Keyword.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
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
:returning
- selects which fields to return. It accepts a list of fields to be returned from the database. Whentrue
, returns all fields. Whenfalse
, no extra fields are returned. It will always include all fields inread_after_writes
. Not all databases support this option.:force
- By default, if there are no changes in the changeset,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".
See the "Shared options" section at the module documentation for more options.
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