LoadError Specified 'sqlite3' for database adapter? - ruby-on-rails

My Gemfile looks like this:
source 'https://rubygems.org'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.1.0.0'
gem 'activesupport', '4.0.2'
gem 'jquery-rails', '2.0.2'
gem 'railties', '4.0.2'
gem 'sass-rails', '4.0.1'
gem 'coffee-rails', '4.0.1'
gem 'rails_12factor'
gem 'actionpack', '4.0.2'
gem 'sqlite3', '1.3.5'
group :development, :test do
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '4.0.1'
gem 'coffee-rails', '4.0.1'
gem 'uglifier', '1.2.3'
end
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '4.1.0'
gem 'cucumber-rails', '1.2.1', :require => false
gem 'database_cleaner', '0.7.0'
# gem 'launchy', '2.1.0'
# gem 'rb-fsevent', '0.9.1', :require => false
# gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
I run bundle install and bundle update but when I visit my app locally I get this error:
Specified 'sqlite3' for database adapter, but the gem is not loaded.
Add gem 'sqlite3' to your Gemfile.
I tried bundle show sqlite3 and I get /Users/siaW/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/sqlite3-1.3.5 which means I do have the Gem right?
So why isn't it loading?
In my database.yml I have this:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000

I think you may try this:
Go into your Gemfile.lock file and search for the 'sqlite3' entry. You'll note it reads sqlite3 (1.3.8-x86-mingw32). Change that to sqlite3 (1.3.8-x64-mingw32) and then run the bundle install command and everything should work like normal.
from Sqlite3 gem not loading, gem installed and specified in project

I had this on OSX for a spec/dummy app. Without rebuilding the gemset (just rebuilding sqlite3), it was unsuccessful.
I had to do the following to get everything built properly:
rvm gemset empty
brew update
brew install sqlite
bundle update

Related

heroku rake db:migrate is not working in rails 4

When I do rake db:migrate, I get the error,below i have attached my databse.yml and my gem file, tried all the stuff on the internet but the error did'nt get resolved!
rake aborted!
LoadError: cannot load such file -- mysql2
development:
adapter: mysql2
encoding: utf8
database: demo_project_development
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
host: localhost
test:
adapter: mysql2
database: demo_project_test
database: db/development.mysql2
username: root
password: root
pool: 5
timeout: 5000
production:
adapter: postgresql
database: demo_project_production
pool: 5
timeout: 5000
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :production do
gem 'pg', '0.20'
gem 'rails_12factor'
end
group :test do
gem 'byebug'
gem 'mysql2', '>= 0.3.13', '< 0.5'
end
gem 'spring'
gem 'devise'
gem "cancan"
gem 'ckeditor', '4.1.3'
gem "nested_form"
gem "paperclip", "~> 5.0.0"
gem 'bootstrap-sass', '~> 3.3.6'
gem 'kaminari'
gem 'ratyrate'
gem 'thinking-sphinx', '~> 3.3.0'
gem 'delayed_job_active_record'
gem 'rails-api'
gem 'active_model_serializers', '~> 0.10.6'
group :development do
gem 'web-console', '~> 2.0'
gem 'mysql2', '>= 0.3.13', '< 0.5'
end
Below is my application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module DemoProject
class Application < Rails::Application
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :delayed_job
config.api_only = false
end
end
config/envoirment.rb
require File.expand_path('../application', __FILE__)
Rails.application.initialize!
boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
I was able to reproduce your error in a dummy Rails 4.2.6 app with your Gemfile and database.yml. I couldn't even push the app to Heroku without raising LoadError: cannot load such file -- mysql2.
I fixed the problem by removing the mysql2 gem from the development and test groups and adding it to the default group. My guess is that running bundle install locally adds it as a dependency to Gemfile.lock which is checked into git and pushed to Heroku. Heroku doesn't install the gem and when the app tries to require it a LoadError is raised.
group :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
end
gem 'mysql2', '>= 0.3.13', '< 0.5'
A couple notes: if you want to add one gem to two or more specific groups, do not specify them separately. This just means there are two places where you're going to have to remember to change your version dependencies. So instead of this
group :test do
gem 'byebug'
gem 'another_gem'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'another_gem'
end
do this
group :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
end
group :development, :test do
gem 'another_gem'
end
Also, you shouldn't use MySQL locally and PostgreSQL in production, just use Postgres for both. They are not fully compatible, so you are bound to run into confusing issues down the road. Your best solution to this problem would be to remove mysql2 altogether and configure your dev and test databases for Postgres.

RAILS TUTORIAL & HEROKU DEPLOYMENT :rake aborted! Gem::LoadError: Specified 'postgresql' for database adapter,

Hi I've been following www.railstutorial.org's tutorial, when I need to deploy my code to heroku and do 'heroku run rake db:migrate' it keep throw me the same error
rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
this is my gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'bcrypt', '3.1.7'
gem 'faker', '1.4.2'
gem 'carrierwave', '0.10.0'
gem 'mini_magick', '3.8.0'
gem 'fog', '1.36.0'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.1'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor'
gem 'puma', '3.1.0'
end
I've done bundle install and it all works fine on my local machine, just can't update my heroku db, and my heroku app always give me 'Application Error'.
all my code is actually in github and you can see it here
thanks in advance!
Looks like the gem pg is only loaded in your local environment. Delete your Gemfile.lock file and run bundle install then commit to your github -> git add -A, git commit -m "some message", git push, then do git push heroku master
heroku run rake db:migrate should work now.
It is because of your database.yml file in the config folder.
In this file you are using sqlite3 as your database. you need to modify this file if you want to use postgresql.
For example you would replace all code in the database.yml file with the following if you want to use postgresql for development, test and production:
development:
adapter: postgresql
database: NAMEOFDATABASE_development
pool: 5
timeout: 5000
test:
adapter: postgresql
database: NAMEOFDATABASE_test
pool: 5
timeout: 5000
production:
adapter: postgresql
database: NAMEOFDATABASE_production
pool: 5
timeout: 5000
If you only want to use postgresql for production the only modify the production code
Also, you should declare your ruby version at the top of your gemfile by placing this: ruby '2.3.1' at the top. Obviously change the version if you are using a different version
See This and This for more details on converting your database from sqlite to postgresql

Rails: Change my development database from sqlite to postgresql

I've got a Rails app which currently uses sqlite in development and postgresql in production on Heroku. However, I'm trying to change this so that I use postgresql in development.
My Gemfile currently looks like this:
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'bcrypt', '3.1.7'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'jquery-ui-rails', '~> 4.2.1'
gem 'turbolinks', '2.3.0'
gem 'jquery-turbolinks'
gem 'jbuilder', '2.2.3'
gem 'sdoc', '0.4.0', group: :doc
gem 'chart-js-rails'
gem 'gon'
gem 'lodash-rails'
group :development, :test do
#gem 'sqlite3', '1.3.9'
gem 'pg', '0.17.1'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '3.1.0'
end
So in the development group I've replaced the 'sqlite3' gem with the 'pg' gem.
I've modified the database.yml to look something like this:
default: &default
adapter: postgresql
host: localhost
pool: 5
timeout: 5000
development:
<<: *default
database: abc_development
username: abc
password: password1
test:
<<: *default
database: abc_test
username: abc
password: password1
production:
<<: *default
database: abc_production
username: abc
password: password1
I've executed bundle install --without production however, when I try bundle exec rake db:create db:migrate I receive the following error:
Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
As far I see Rails is right: you specified the pg gem only in group production, but you said you want to use it also in development.
So just move it from the production group to the beginning of your Gemfile

Specified sqlite3 for database adapter but gem is not loaded

I'm new to Rails and was trying to launch my server. Running the command rails server generated an error
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).
I looked at many of these previous questions and none seems to solve it. Here is my Gemfile code
if RUBY_VERSION =~ /1.9/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
source 'https://rubygems.org'
gem 'rails', '~> 4.2.0'
gem 'ey_config'
gem 'rails_autolink'
gem 'simple_form'
# Assets
gem 'jquery-rails'
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
platform :ruby do
gem 'mysql2'
gem 'pg'
gem 'activerecord-postgis-adapter', '3.0.0.beta2'
gem 'sqlite3'
gem 'newrelic_rpm'
gem 'unicorn'
gem 'puma'
gem 'json'
gem 'minitest'
gem 'psych'
gem 'racc'
end
platforms :jruby do
ar_jdbc_version = '~> 1.3'
gem 'activerecord-jdbc-adapter', ar_jdbc_version
gem 'activerecord-jdbcmysql-adapter', ar_jdbc_version
gem 'activerecord-jdbcpostgresql-adapter', ar_jdbc_version
gem 'activerecord-jdbcsqlite3-adapter', ar_jdbc_version
gem 'jdbc-mysql', :require => false
gem 'jdbc-sqlite3', :require => false
gem 'jdbc-postgres', :require => false
gem 'jruby-openssl'
gem 'trinidad'
end
platform :rbx do
gem 'rubysl'
gem 'rubysl-test-unit', :require => false
end
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'tzinfo-data'
end
And here is my database.yml file
# SQLite version 3.x
# gem install sqlite3
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000`
Here is the code in my rakefile:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
Listr::Application.load_tasks
task :travis => ['db:create:all', 'db:migrate', :default]
It will be also appreciated for providing any explanation accompanying the answer. Thanks.
Your sqlite3 gem is not being loaded in production, because of the version mismatch. so, update you gemfile as gem 'sqlite3', '~> 1.3.13' it'll work. Keep it under group :production, :test It'll be good when you push your code to heroku.
Your sqlite3 gem is not being loaded, because of where you've got it in the Gemfile. Take it out of platform :ruby do and place it outside of that block , May be right under gem 'rails', '~> 4.2.0'

Could not load 'active_record/connection_adapters/sqlite3_adapter'. Windows 7 local server

I have been going through the Rails tutorial, https://www.railstutorial.org/book/static_pages, and the first two chapters went fine. I installed Rails on my Laptop made a hello world app and a toy with the scaffold generator. There were some things I had to do differently because I am doing everything locally instead of on a cloud environment and because I am using Windows 7, but I got everything to work.
When I tried running a local server and going to my http://localhost:3000/static_pages/home page, i got the error in the Title. The following is the full error message:
Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.
I figure it must be something with the Gemfile because that was the only difference between this and my other apps, I'm just not sure what to change. I included the Gemfile and the database.yml. Thanks in advance.
#Gemfile - sample_app
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'faker', '1.4.2'
gem 'carrierwave', '0.10.0'
gem 'mini_magick', '3.8.0'
gem 'fog', '1.23.0'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '2.11.1'
end
Database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 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
In your Gemfile, you specify sqlite3 (the 'sqlite3' gem) for development and testing, and postgres (the 'pg' gem) for production.
But in your database.yml, you've specified the production database as sqlite3:
<database.yml>
...
production:
<<: *default
database: db/production.sqlite3
which will try to use the default adapter (sqlite3) and will look for a database file with .sqlite3 as the file extension.
You need to change your database.yml so that it uses a postgres database:
production:
adapter: postgresql
encoding: unicode
pool: 5
database: db/production
(Make sure you have postgres running on your machine either as an app or as a service.)
Or you can change your Gemfile so that you use SQLite3 for production, too.
If you tried to run the production version on your Windows machine but perhaps were running a development or test version on your OSX machine, this could be the problem.

Resources