SQLite3 Error - cannot rollback - no transaction is active

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

SOLVED (see below)

When I'm trying to create or destroy a record (running on SQLite3) in my Ruby on Rails app (or in console as well) I'm getting an SQL Exception:

 irb(main):014:0> ApiCache.all => [] irb(main):015:0> ApiCache.create(:hash => 'qwe', :contents => 'asd') SQLite3::SQLException: cannot rollback - no transaction is active         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/sqlite3-1.3.3-x86-mingw32/lib/sqlite 3/database.rb:97:in `close'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/sqlite3-1.3.3-x86-mingw32/lib/sqlite 3/database.rb:97:in `prepare'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/sqlite3-1.3.3-x86-mingw32/lib/sqlite 3/database.rb:134:in `execute'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/sqlite3-1.3.3-x86-mingw32/lib/sqlite 3/database.rb:517:in `rollback'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /connection_adapters/sqlite_adapter.rb:168:in `rollback_db_transaction'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /connection_adapters/abstract/database_statements.rb:176:in `transaction'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /transactions.rb:207:in `transaction'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /transactions.rb:290:in `with_transaction_returning_status'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /transactions.rb:240:in `save'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /transactions.rb:251:in `rollback_active_record_state!'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /transactions.rb:239:in `save'         from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.6/lib/active_record /base.rb:498:in `create'         from (irb):15         from :0 irb(main):016:0> ApiCache.all => [#] 

The record saves into the database but also throws this kind of error. I tried to delete my DB then create and migrate it again, but it doesn't appear to make any good.

I would really appreciate any ideas on how to fix it. I've never faced such thing before =(

SOLUTION

I've destroyed my model and created another one. Must be some conflict in attribute names, as the new one now has fp:string contents:text and works nicely. Might be the word hash that ruined SQLite =) Thank y'all!

回答1:

Holy smokes... The situation is in-fact that the field name "hash" causes sqlite3 to throw this very unhelpful error. Tricky one...



回答2:

It is a good idea to never to make an instance method named "hash". This is used by a ton of ruby objects for comparison and equality. No matter weather your using SQLite, or anything, a custom hash method should only be done when you know what you want from it's results. In this case ActiveRecord made the method for you. I would rename the column :)



回答3:

Perhaps you should try to look here: Rails Tests Fail With Sqlite3 Looks like they may have found a workaround.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!