I am using Sequel with Postgres. I notice on inserting new records while using the `save` method, Sequel::Model uses returning *. However, it does not use returning * on update. Is there a way to make it the default on updating models too?
On Tuesday, November 6, 2012 8:07:55 PM UTC-8, Robert Sosinski wrote: > Hi,
> I am using Sequel with Postgres. I notice on inserting new records while > using the `save` method, Sequel::Model uses returning *. However, it does > not use returning * on update. Is there a way to make it the default on > updating models too?
Sequel refreshes the model when creating, but not when updating. There isn't a simple way for it to using returning * on updates, but if you need to refresh after updates, you can call refresh in an after_update hook. The only real reason to do that is if you have an update trigger or something else that changes the values stored in the database, and you want those new values reflected in the model object.
The main reason for this design is that model refreshing is usually necessary on creation to get values that weren't set in the INSERT call, while it is only useful during updates in rare cases.
Note that it should be fairly easy to write a plugin that uses RETURNING * when updating and sets the retrieved values in the model object, if you don't want the performance hit of another query to refresh.
This lets me update the record and get back the row from the database after triggers. Would it be possible plug the `returning` method into the save method when using updates? If it is more complicated then that, could you let me know why.
On Wednesday, November 7, 2012 1:15:16 AM UTC-5, Jeremy Evans wrote:
> On Tuesday, November 6, 2012 8:07:55 PM UTC-8, Robert Sosinski wrote:
>> Hi,
>> I am using Sequel with Postgres. I notice on inserting new records while >> using the `save` method, Sequel::Model uses returning *. However, it does >> not use returning * on update. Is there a way to make it the default on >> updating models too?
> Sequel refreshes the model when creating, but not when updating. There > isn't a simple way for it to using returning * on updates, but if you need > to refresh after updates, you can call refresh in an after_update hook. > The only real reason to do that is if you have an update trigger or > something else that changes the values stored in the database, and you want > those new values reflected in the model object.
> The main reason for this design is that model refreshing is usually > necessary on creation to get values that weren't set in the INSERT call, > while it is only useful during updates in rare cases.
> Note that it should be fairly easy to write a plugin that uses RETURNING * > when updating and sets the retrieved values in the model object, if you > don't want the performance hit of another query to refresh.
> This lets me update the record and get back the row from the database > after triggers. Would it be possible plug the `returning` method into the > save method when using updates? If it is more complicated then that, could > you let me know why.
> Really appreciate the help.
It's not more complicated than that. You probably want to override Model#_update_dataset to do super.returning('*'.lit) and Model#_update_without_checking to do (s = super; set_values(s.first); s.length). That's pretty trivial to do using a plugin (or just adding the instance methods to Sequel::Model), which is the route I suggest you take if you don't want to have a separate query to refresh.