Rails 4 fails on sqlite3

2019-09-09 03:55发布

问题:

I get this error when I run a new empty rails (4.0) site:

Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile.

I have a valid sqlite3 gem built with the devkit. I made a dumb test for that:

w:...> ruby   hello.rb
hello world
test.db is done.

Which also creates an empty database, called "test.db" as shown below. Simple ruby script:

  require "sqlite3"

  puts "hello world"

  # Open a database
  db = SQLite3::Database.new "test.db"

  puts "test.db is done."

My conclusion is that the sqlite3 gem is working OK and that there's something amiss inside rails some place. Suggestions or explanations welcome.

Bit of an update following the comment about Gemfile:

# Use sqlite3 as the database for Active Record
gem 'sqlite3', group: [:development, :test]

In addition, I used the bundle show command, and I find this strange:

w:...> bundle show sqlite3
Could not find gem 'sqlite3'.
Did you mean sqlite3?

When I do a bundle install the output list does not include sqlite3. I should add the Gemfile and everything else was generated right out of the box by a "rails new train" command (project name, 'train' of course).

Environment is an install of Ruby v2, Rails v4 on Windows 7 64-bit PC. I have PIK to manage different versions of ruby. Suggestions welcome, thanks in advance, Will.

Everything is/should be fresh-out-of-the-box Rails 4/Ruby 2 configuration. I'm adding the server's stack dump also, since in theory a fresh install should be working.

Gem::LoadError (Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile.):
  activerecord (4.0.0) lib/active_record/connection_adapters/connection_specification.rb:58:in `rescue in resolve_hash_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/connection_specification.rb:55:in `resolve_hash_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/connection_specification.rb:30:in `spec'
  activerecord (4.0.0) lib/active_record/connection_handling.rb:39:in `establish_connection'
  activerecord (4.0.0) lib/active_record/railtie.rb:175:in `block (2 levels) in <class:Railtie>'
  activesupport (4.0.0) lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
  activesupport (4.0.0) lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
  activesupport (4.0.0) lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
  activesupport (4.0.0) lib/active_support/lazy_load_hooks.rb:44:in `each'
  activesupport (4.0.0) lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
  activerecord (4.0.0) lib/active_record/base.rb:322:in `<module:ActiveRecord>'
  activerecord (4.0.0) lib/active_record/base.rb:22:in `<top (required)>'
  activerecord (4.0.0) lib/active_record/query_cache.rb:50:in `restore_query_cache_settings'
  activerecord (4.0.0) lib/active_record/query_cache.rb:43:in `rescue in call'
  activerecord (4.0.0) lib/active_record/query_cache.rb:32:in `call'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__398267657__call__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  B:/lang/ruby/v02.00/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  B:/lang/ruby/v02.00/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  B:/lang/ruby/v02.00/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

回答1:

Two bits of news to report. I was passed an explanation for this on the PIK mailing list:

[Luis Lavena] You don't indicate which version of Ruby 2.0 are you using (the exact output of "ruby -v"), but I will assume is x64 bit, correct?

If that is the case, then you're affected by a bug in Bundler. The same was reported here:

  • https://github.com/sparklemotion/sqlite3-ruby/issues/109

Yes, the problem is happening with Ruby v2 64-bit

 w:...> ruby -v
 ruby 2.0.0p195 (2013-05-14) [x64-mingw32]

 w:...> bundle -v
 Bundler version 1.3.5

Reading the sparklemotion post the error lies with Bundler. To be fathomed I guess.

On the plus side. I used Rails 4 with Ruby 2 to make the rails project and can run the server to use SQLite3 using JRuby by changing the Gemfile to use the jdbc driver, as shown:

# Use sqlite3 as the database for Active Record
group :development, :test do
  #  gem 'sqlite3'
  gem 'activerecord-jdbc-adapter'
  gem 'jdbc-sqlite3'
end

It almost looks like Bundler has a thing against SQLite3 doesn't it?

One last thing. The Sqlite3 gem build OK with Ruby v2 and works, as demonstrated with my toy test program. Since it is just a Gem ... the Bundler problem may pop-up for some other gems -- Be alert, probably best to upgrade as soon as you see the problem:

w:...> ruby -S gem update bundler --prerelease
Updating installed gems
Updating bundler
Fetching: bundler-1.4.0.rc.1.gem (100%)
Successfully installed bundler-1.4.0.rc.1
Parsing documentation for bundler-1.4.0.rc.1
Installing ri documentation for bundler-1.4.0.rc.1
Installing darkfish documentation for bundler-1.4.0.rc.1
Done installing documentation for bundler after 9 seconds
Gems updated: bundler

Just check the list

w:...> bundle list
Gems included by the bundle:
    :
  * sprockets (2.10.0)
  * sprockets-rails (2.0.1)
  * sqlite3 (1.3.8)
  * thor (0.18.1)
    :

Works a treat!

:-)



回答2:

Dou you have:

gem 'sqlite3'

or something like this

gem 'sqlite3', group: [:development, :test]

in your Gemfile?