ecto icon indicating copy to clipboard operation
ecto copied to clipboard

Add returning option to Repo.update

Open c-spencer opened this issue 11 years ago • 7 comments

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.

c-spencer avatar Dec 22 '14 21:12 c-spencer

Well, if we are going down this road, we should just always return the updated model. And the same for insert.

josevalim avatar Dec 22 '14 22:12 josevalim

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.

ericmj avatar Dec 23 '14 01:12 ericmj

On master it returns the record already!

josevalim avatar Dec 27 '14 11:12 josevalim

Master and v0.3.0.

josevalim avatar Dec 27 '14 11:12 josevalim

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.

isadon avatar Aug 17 '19 08:08 isadon

update does not support the :returning option (it is not documented as such either). I will push this feature to master in a sec.

josevalim avatar Aug 17 '19 09:08 josevalim

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!

isadon avatar Aug 17 '19 16:08 isadon