# Merb + Sequel + mysql transactions

**URL:** <https://www.ruby-forum.com/t/merb-sequel-mysql-transactions/166085>\
**Category:** Ruby\
**Created:** [April 21, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/merb-sequel-mysql-transactions/166085 "2009-04-21T19:28:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ulag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/3bc359/32.png) [@ulag](https://www.ruby-forum.com/u/ulag)\
**Post date:** [April 21, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/merb-sequel-mysql-transactions/166085/1 "2009-04-21T19:28:51Z")

</div>

I don’t seem to get the transaction to rollback the inserts performed.  
Below are my models

class Group \< Sequel::Model  
one\_to\_many :members, :class =\> :Member, :conditions =\> {:is\_member =\>  
1}  
one\_to\_many :non\_members, :class =\> :Member, :conditions =\>  
{:is\_member =\> 0}

def insert  
db.transaction do  
self.save  
self.members.each do |member|  
member.insert  
end  
# force error to check the transaction rolling back  
raise(Sequel::Error::Rollback)

```
self.non_members.each do |member|
 member.insert
end

```

end

end

end

class Member \< Sequel::Model  
def insert  
db.transaction do  
self.save  
end  
end  
end

With this code, when I call group.insert I see the below sql in log.  
However, looking in the tables associated, I do see that the group  
record and one member record is inserted. Shouldn’t it rollback all the  
inserts in case of errors. I tried model.save(:transaction =\> true) but  
that did not change this behavior either. Am I missing something?

2009-04-21T10:00:33.620-07:00|INFO BEGIN  
2009-04-21T10:00:33.620-07:00|INFO INSERT INTO `groups` (`id`) VALUES  
(‘group-1’)  
2009-04-21T10:00:33.622-07:00|INFO INSERT INTO `members` (`status`,  
`account_id`, `token`, `is_member`, `token_expires_at`, `type`,  
`group_id`) VALUES (‘confirmed’, ‘12’, NULL, 1, NULL, ‘admin’,  
‘group-1’)  
2009-04-21T10:00:33.623-07:00|INFO ROLLBACK

---

<div class="post-metadata">

**Author:** ![ulag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/3bc359/32.png) [@ulag](https://www.ruby-forum.com/u/ulag)\
**Post date:** [April 21, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/merb-sequel-mysql-transactions/166085/2 "2009-04-21T21:41:09Z")

</div>

Hi Ulag,

On Tue, Apr 21, 2009 at 10:29 AM, Ulag S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> db.transaction do  
> end  
> end  
> 2009-04-21T10:00:33.620-07:00|INFO INSERT INTO `groups` (`id`) VALUES  
> (‘group-1’)  
> 2009-04-21T10:00:33.622-07:00|INFO INSERT INTO `members` (`status`,  
> `account_id`, `token`, `is_member`, `token_expires_at`, `type`,  
> `group_id`) VALUES (‘confirmed’, ‘12’, NULL, 1, NULL, ‘admin’,  
> ‘group-1’)  
> 2009-04-21T10:00:33.623-07:00|INFO ROLLBACK

The ROLLBACK rolls back everything back to the BEGIN. Looks like it’s  
behaving correctly.

Try posting to the sequel mailing list for a more informed response:  
[http://groups.google.com/group/sequel-talk](http://groups.google.com/group/sequel-talk)

jeremy
