Add returning option to Repo.update
Would be good to be able to specify either the whole row, or selected columns for efficiency, where you're expecting certain columns to be set or modified by the database (through triggers or what-have-you).
Proposal:
# Same return as an update/get pair at present:
Repo.update(post, returning: :row)
# Returns post with selected fields updated with RETURNING
Repo.update(post, returning: [:last_modified])
Currently update/2 returns :ok | no_return, so that would change to Ecto.Model.t | :ok | no_return, dependent upon the presence of the returning option.
Thoughts? Happy to look at implementing, will need to familiarise myself with the code.
Well, if we are going down this road, we should just always return the updated model. And the same for insert.
Repo.update could take an option to return the full model. If you want to return specific fields Repo.update_all could take an optional argument accepting an ecto query expression just like :select in a normal ecto query.
On master it returns the record already!
Master and v0.3.0.
Not sure if the changes from this issue ended up being added to Repo.update/2 but I am making a call as such below and the returning keyword list is not being applied.
result =
%Schema.User{id: id_int}
|> cast(conn.params, valid_keys)
|> Schema.User.validate_changeset()
|> Repo.update(returning: [:username, :gender, :photo1_url])
I can see in the log output that the Ecto is applying the 'returning' based on the fields in the schema that have read_after_writes: true. I would expect that if you specify a returning clause here that the setting here would take precedence.
update does not support the :returning option (it is not documented as such either). I will push this feature to master in a sec.
Hi @josevalim Thanks for the quick reply. I agree that it is not documented as having that feature but given the other update functions had it, it left me wondering why update/2 didn’t. Thanks!