Feber Castellon
Feber Castellon

Reputation: 579

Error loading the 'sqlite3' Active Record adapter. when I deploy in Heroku

I have a problem when I am deploying in Heroku:

/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:408:in `block (2 levels) in replace_gem': Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? sqlite3 is not part of the bundle. Add it to your Gemfile. (LoadError)

I tried to follow guides where I have to downgrade SQLite3, but is not working, also I read solutions where I have to use Rails 5.2, but I am at 6.0 right now. Is there a possiblity to make it work with Rails 6?

This is my Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'


gem 'rails', '6.0.1'
gem 'autoprefixer-rails', '9.6.1.1'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '5.0.0'
gem 'jquery-rails', '4.3.5'
gem 'mini_magick', '4.9.5'
gem 'will_paginate', '3.2.1'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.4.1'
gem 'puma', '4.3.1'
gem 'font-awesome-rails', '4.7.0.5'
gem 'sass-rails', '6'
gem 'webpacker', '4.0'
gem 'turbolinks', '5'
gem 'jbuilder', '2.9.1'
gem 'rubocop', '0.77.0'
gem 'bootsnap', '1.4.2', require: false

group :development, :test do
  gem 'sqlite3', '~> 1.3.6'
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

group :development do
  gem 'web-console', '3.3.0'
  gem 'listen', '3.2.0'
  gem 'spring'
  gem 'spring-watcher-listen', '2.0.0'
end

group :test do
  gem 'capybara',           '3.28.0'
  gem 'selenium-webdriver', '3.142.4'
  gem 'webdrivers',         '4.1.2'
end

group :production do
  gem 'pg',  '0.20.0'
  # gem 'fog', '1.42'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

and this is my database.yml:

# SQLite. Versions 3.8.0 and up are supported.
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

Upvotes: 4

Views: 2168

Answers (5)

Alvaro Pedraza
Alvaro Pedraza

Reputation: 1292

I had a similar problem and changing the adapter only in production worked for me, so database.yml is like:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  adapter: postgresql
  database: db/production.sqlite3

Hope it works for somebody else.

Upvotes: 0

max
max

Reputation: 102368

You should remove SQLite from your application and use Postgres in development, testing and production. There are many small incompatibilities between RDBMS's and you don't want to discover them when you push your app to production.

1. Install Postgres on the local system.

How to do this depends on your system. OS-X and Windows have simple installers. On Linux you would install it through your package manager.

2. Remove the sqlite gem.

# remove
gem 'sqlite3', '~> 1.3.6'

# add
gem 'pg', '~> 0.18.4' # check rubygems.org for the latest version

Run bundle update to regenerate the Bundle.lock.

3. Configure database.yml

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: 'my_app_development'

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: 'my_app_test'

# On Heroku you don't need the production section as it provides everything through 
# ENV["DATABASE_URL"]
# Heroku sets pretty good defaults as well so YAGNI.

4. Commit and push

Upvotes: 7

jvillian
jvillian

Reputation: 20253

You specify sqlite3 as the default adapter under the default: section of your database.yml:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

which is causing your error since sqlite3 isn't installed in production.

You should follow the link that Sebastian Palma provides in the comments. There, you will find a discussion of how to configure your app to use PostgreSQL on Heroku.

Upvotes: 1

Unixmonkey
Unixmonkey

Reputation: 18784

Heroku doesn't natively support SQLite3, and although I see you added the pg gem in production, you'll also need to configure your Rails application to load its database from PostgreSQL by editing config/database.yml to connect to PostgreSQL.

Upvotes: 0

Rosel Sosa Gonzalez
Rosel Sosa Gonzalez

Reputation: 122

Heroku doesn't natively support SQLite3.

First open your Gemfile and remove this line:

gem 'sqlite3'

Replace with this line:

gem 'pg'

Then run bundle install.

Next you will need to convert your config/database.yml. Open the existing file, which might look something like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

You will need to change the adapter from

adapter: sqlite3

to this:

adapter: postgresql

Note the adapter name is postgresql not postgres or pg. You will also need to change the database: to a custom name. A final version might look something like this:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: my_database_development

test:
  <<: *default
  database: my_database_test

production:
  <<: *default
  database: my_database_production

Once you’ve installed the pg gem and migrated your config/database.yml file you will need to create your database and run any pre-existing migrations.

Now when you push to Heroku using Rails, a development grade PostgreSQL instance will be provisioned and connected to your application automatically. If you’re not using Rails, you may need to manually add the PostgreSQL addon by running

heroku addons:create heroku-postgresql

Upvotes: 0

Related Questions