ActiveRecord的错误:SAVEPOINT active_record_1不存在 [英] ActiveRecord error: SAVEPOINT active_record_1 does not exist
问题描述
完整的错误是
ActiveRecord::StatementInvalid: Mysql2::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1
我写一个单元测试,并收到这个错误,每当我尝试创建一个新的ActiveRecord对象 - 但只有在某一点。出现这种情况的这些行后:
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 => 'foo@bar.com')
和一切工作正常。不过,如果我尝试写我呼吁的ActiveRecord :: Base.connection.execute经过上面的线,然后我得到上述这一SAVEPOINT错误。我也试图把我的执行语句在一个事务中,但是这并没有帮助。我很为难。
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.
仅供参考 - 我用Rails 3.2.8
FYI - I'm using Rails 3.2.8
推荐答案
您正在使用MySQL DDE语句(创建/删除/截断表),这将导致的implicit提交。
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).
要解决这个问题,你可以<一href="http://stackoverflow.com/questions/3853098/turn-off-transactional-fixtures-for-one-spec-with-rspec-2">turn关闭交易并使用 DatabaseCleaner (截断模式)。
To get around this, you can turn off transactions and use DatabaseCleaner (truncation mode).
这篇关于ActiveRecord的错误:SAVEPOINT active_record_1不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!