Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord error: SAVEPOINT active_record_1 does not exist

Tags:

The full error is

ActiveRecord::StatementInvalid: Mysql2::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1 

I am writing a unit test and getting this error whenever I try to create a new ActiveRecord object -- but only after a certain point. This occurs after these lines:

ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS foo" ActiveRecord::Base.connection.execute "CREATE TABLE foo (id INTEGER PRIMARY KEY)" 

(The table 'foo' will be populated with data if my test succeeds)

Before the above lines, I can write something like

User.create(email => '[email protected]') 

and everything works fine. However, if I try writing the above line after my call to ActiveRecord::Base.connection.execute, then I get this SAVEPOINT error described above. I've also tried putting my execute statements within a transaction, but that didn't help. I'm stumped.

FYI - I'm using Rails 3.2.8

like image 810
Duke Silver Avatar asked Oct 31 '12 15:10

Duke Silver


2 Answers

To solve this issue..

config.use_transactional_fixtures = false

like image 22
vel pradeep.MS Avatar answered Sep 29 '22 05:09

vel pradeep.MS


You are using Mysql DDE statements (create/drop/truncate table) which will result in an implicit commit.

Because of the implicit commit, all savepoints of the current transaction are deleted (Refer to above documentation).

To get around this, you can turn off transactions and use DatabaseCleaner (truncation mode).

like image 162
awaage Avatar answered Sep 29 '22 04:09

awaage