SQLite3 Error - cannot rollback - no transaction i

2019-01-27 21:50发布

问题:

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
=> [#<ApiCache id: 9, hash: "qwe", contents: "asd", created_at: "2011-05-09 13:09:31", updated_at: "
2011-05-09 13:09:31">]

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.