当我尝试在我的Ruby on Rails应用程序(或在控制台中)创建或销毁记录(在SQLite3上运行)时,我遇到了SQL异常:
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">]
记录保存到数据库中但也会抛出此类错误。我试图删除我的数据库,然后再创建并迁移它,但它似乎没有任何好处。
我真的很感激有关如何修复它的任何想法。我以前从未遇到过这样的事情=(
我摧毁了我的模型并创建了另一个模型。必须在属性名称中存在一些冲突,因为新的属性名称现在具有 fp:string contents:text 并且运行良好。可能是破坏SQLite的哈希这个词=)谢谢你们!
答案 0 :(得分:9)
神圣抽烟......实际情况是,字段名称“hash”会导致sqlite3抛出这个非常无用的错误。棘手的......
答案 1 :(得分:1)
永远不要创建名为“hash”的实例方法。这被大量的红宝石物体用于比较和平等。无论你使用SQLite还是其他任何东西,只有当你知道你想要的结果时,才应该使用自定义哈希方法。在这种情况下,ActiveRecord为您制作了该方法。我会重命名专栏:)
答案 2 :(得分:0)
也许您应该尝试查看此处:Rails Tests Fail With Sqlite3看起来他们可能找到了解决方法。