Heroku 没有看到 gem 'pg'

Heroku doesn't see gem 'pg'

需要帮助。

当我尝试制作时 user@X220:~/rails_projects/sample_app$ heroku run rake db:migrate

有一个错误:

Running rake db:migrate on limitless-fjord-69900.... up, run.2816 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).
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:177:in rescue in spec' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:174:inspec' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in establish_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:inblock (2 levels) in ' /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in instance_eval' /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:inexecute_hook' /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:45:in block in run_load_hooks' /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:ineach' /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in run_load_hooks' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/base.rb:315:in' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/base.rb:26:in <top (required)>' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/tasks/mysql_database_tasks.rb:8:in' . . .
Tasks: TOP => db:migrate => db:load_config (See full trace by running task with --trace)

我的 Gemfile 是:

'source 'https://rubygems.org'  
ruby '2.2.0'  
gem 'activerecord', '~> 4.2', '>= 4.2.6'  
gem 'rails', '4.2.6'  
group :development, :test do  
    gem 'byebug'  
  gem 'sqlite3', '~> 1.3', '>= 1.3.11'  
  gem 'rspec-rails', '~> 2.8'  
end  
group :test do  
  gem 'selenium-webdriver', '2.35.1'  
  gem 'capybara', '2.1.0'  
end  
gem 'sass-rails', '~> 5.0'  
gem 'uglifier', '>= 1.3.0'  
gem 'coffee-rails', '~> 4.1.0'  
gem 'therubyracer', platforms: :ruby  
gem 'jquery-rails'  
gem 'turbolinks'  
gem 'jbuilder', '~> 2.0'  
group :doc do  
  gem 'sdoc', '~> 0.4.0', require: false   
end   
gem 'unicorn'  
group :development do  
  gem 'web-console', '~> 2.0'  
  gem 'spring'  
end  
group :production do  
  gem 'rails_12factor'  
  gem 'pg'  
end

怎么了?

从您的 Gemfile 中删除或评论 gem 'sqlite3',即使它在 development 组中。 我不知道为什么,但是当 Heroku 包含 sqlite gem 外部生产时,Heroku 不会 运行 您的应用程序。

如果您在开发中使用 SQLite,则每次部署到 Heroku 时都必须注释 gem 'sqlite' 并在部署后取消注释。
更好的方法是在开发中使用 Postgres。

如前所述,这可能与您的 sqlite gem 有关。

如果您仍想使用 sqlite,您可以设置忽略该组。来自 Heroku 文档:

To specify groups of gems to not to be installed, you can use the BUNDLE_WITHOUT config var.

$ heroku config:set BUNDLE_WITHOUT="development:test"