【问题标题】:Rails 4 fails on sqlite3Rails 4 在 sqlite3 上失败
【发布时间】:2013-10-29 09:02:02
【问题描述】:

当我运行一个新的空 rails (4.0) 站点时出现此错误:

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

我有一个使用 devkit 构建的有效 sqlite3 gem。我为此做了一个愚蠢的测试:

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

这也创建了一个空数据库,名为“test.db”,如下所示。简单的红宝石脚本:

  require "sqlite3"

  puts "hello world"

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

  puts "test.db is done."

我的结论是 sqlite3 gem 工作正常,并且在某个地方的 rails 内部有些问题。欢迎提出建议或解释。

在关于 Gemfile 的评论之后更新一点:

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

另外,我使用了bundle show命令,发现这个奇怪

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

当我执行 bundle install 时,输出列表不包括 sqlite3。我应该添加 Gemfile,其他所有内容都是通过“rails new train”命令(项目名称,当然是“train”)直接生成的。

环境是在 Windows 7 64 位 PC 上安装 Ruby v2、Rails v4。我有 PIK 来管理不同版本的 ruby​​。欢迎提出建议,在此先感谢,Will。

一切都是/应该是全新的 Rails 4/Ruby 2 配置。我也在添加服务器的堆栈转储,因为理论上全新安装应该可以工作

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'

【问题讨论】:

    标签: sqlite rubygems ruby-on-rails-4


    【解决方案1】:

    要报道的两条新闻。我在 PIK 邮件列表中获得了对此的解释:

    [Luis Lavena] 您没有指明您使用的是哪个版本的 Ruby 2.0(“ruby -v”的确切输出),但我假设是 x64 位,对吗?

    如果是这种情况,那么您会受到 Bundler 中的错误的影响。这里也有同样的报道:

    是的,问题出在 Ruby v2 64 位

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

    阅读sparklemotion 帖子,错误在于 Bundler。我猜想是被理解了。

    有利的一面。我使用 Rails 4 和 Ruby 2 来制作 rails 项目,并且可以通过将 Gemfile 更改为使用 jdbc 驱动程序来运行服务器以使用 JRuby 使用 SQLite3,如下所示:

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

    看起来 Bundler 似乎对 SQLite3 有抵抗力,不是吗?

    最后一件事。使用 Ruby v2 构建的 Sqlite3 gem 可以正常工作,如我的玩具测试程序所示。由于它只是一个 Gem ... Bundler 问题可能弹出一些其他 gem -- 请注意,最好在看到问题后立即升级:

    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
    

    只需查看列表

    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)
        :
    

    工作愉快!

    :-)

    【讨论】:

      【解决方案2】:

      你有:

      gem 'sqlite3'
      

      或者类似的东西

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

      在您的 Gemfile 中?

      【讨论】:

      • 试过了——没有变化。我已经更新了问题信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多