Sequel Model Transaction block is not rollbacked when save fails(because of validation) and raise_on_save_failure = false

StackOverflow https://stackoverflow.com/questions/22775864

Question

These are the models :

class User < Sequel::Model
  self.raise_on_save_failure = false
end

class Addresses < Sequel::Model
  many_to_one: user
  self.raise_on_save_failure = false
end


address = Addresses.find(id: 1)

user = address.user

DB.transaction do
   address.delete
   user.save
end

In the above code if user.save returns nil as some validation fails the transaction is not rollback and that address is still deleted.

Is it possible if user.save fails, address.delete is rollbacked where raise_on_save_failure = false.

The transaction does rollback if raise_on_save_failure = true but dont want to set it true.

Was it helpful?

Solution

This is expected behavior. If you want to explicitly rollback in save fails even if raise_on_save failure is false, you have two options. One is enabling exceptions for that particular save:

 DB.transaction do
   address.delete
   user.save(:raise_on_failure=>true)
end

The second is explicitly rolling back if you detect a save failure:

 DB.transaction do
   address.delete
   raise Sequel::Rollback unless user.save
end

Reference Link to Sequel-Talk Google Groups: https://groups.google.com/forum/#!topic/sequel-talk/Y9NuUUNwfyo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top