LoginSignup
13
7

More than 5 years have passed since last update.

Rails 5.1.6 で Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError) というエラーが出たら

Posted at

TL;DR

sqlite3 gem のバージョンが新しすぎる可能性が高いです。バージョンを落とします。

Gemfile
gem 'sqlite3'

Gemfile
gem 'sqlite3', '~> 1.3.0'

に書き換えて、 bundle install を実行

具体的な症状と解決方法

Rails 5.1.6 で新しいアプリケーションを作成、つまり rails _5.1.6_ new test_app した後に、 Scaffold で CRUD な処理を作ろうとすると、

/Users/i.norifumi.homma/RailsApps/test_app% bin/rails g scaffold User email:string last_name:string first_name:string password:string
/Users/i.norifumi.homma/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

のようなエラーが出ることがあります。
bundle install 時にはエラーは出ませんし、ちゃんと Gemfile にも Gemfile.lock にも sqlite3 が記載されています。

そんな時は、 Gemfile を開いて、 sqlite3 のバージョンを 1.3 系に落として、再度 bundle install します。
そうすると、今度は正しく実行できます。

/Users/i.norifumi.homma/RailsApps/test_app% bin/rails g scaffold User email:string last_name:string first_name:string password:string
Running via Spring preloader in process 17420
      invoke  active_record
      create    db/migrate/20190305084938_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      create      app/views/users/_user.json.jbuilder
      invoke  test_unit
      create    test/system/users_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/users.coffee
      invoke    scss
      create      app/assets/stylesheets/users.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

Rails 5.1.6 では sqlite3 のバージョン 1.4 系に対応していないようです。

13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7