首页 > 解决方案 > Heroku 和 ruby​​ on rails:部署有效,但运行应用程序失败执行通常的 sqlite3 错误

问题描述

我知道这个问题很常见,SO 上的很多问题都提出了相同的解决方案,安装 pg 而不是 sqlite3 并将 sqlite3 移动到开发组下。

我已经这样做了。

我正在关注本教程,第一章:https ://www.railstutorial.org/book/beginning

这是我的宝石文件

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

ruby '2.6.3'

gem 'rails',      '6.0.0'
gem 'puma',       '3.12.1'
gem 'sass-rails', '5.1.0'
gem 'webpacker',  '4.0.7'
gem 'turbolinks', '5.2.0'
gem 'jbuilder',   '2.9.1'
gem 'bootsnap',   '1.4.4', require: false

group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug',  '11.0.1', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console',           '4.0.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.1.0'
  gem 'spring-watcher-listen', '2.0.1'
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', '1.1.4'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我做了

bundle install 

然后

git push heroku master

据我了解,这个阶段运作良好。

具体来说,我在进行推送时在日志中看到了这一点

remote:        Using pg 1.1.4

并且没有提及任何类型的 sqlite

问题

当我在浏览器中打开 webapp 时,我可以看到

应用程序错误 应用程序发生错误,无法提供您的页面。如果您是应用程序 > 所有者,请查看您的日志以获取详细信息。

从我看到的cli

Starting process with command `bin/rails server -p 58154 -e production`
...
Process exited with status 1
Exiting
/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)

我完整的 Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.0.0)
      actionpack (= 6.0.0)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.0.0)
      actionpack (= 6.0.0)
      activejob (= 6.0.0)
      activerecord (= 6.0.0)
      activestorage (= 6.0.0)
      activesupport (= 6.0.0)
      mail (>= 2.7.1)
    actionmailer (6.0.0)
      actionpack (= 6.0.0)
      actionview (= 6.0.0)
      activejob (= 6.0.0)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.0.0)
      actionview (= 6.0.0)
      activesupport (= 6.0.0)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.2.0)
    actiontext (6.0.0)
      actionpack (= 6.0.0)
      activerecord (= 6.0.0)
      activestorage (= 6.0.0)
      activesupport (= 6.0.0)
      nokogiri (>= 1.8.5)
    actionview (6.0.0)
      activesupport (= 6.0.0)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.0.0)
      activesupport (= 6.0.0)
      globalid (>= 0.3.6)
    activemodel (6.0.0)
      activesupport (= 6.0.0)
    activerecord (6.0.0)
      activemodel (= 6.0.0)
      activesupport (= 6.0.0)
    activestorage (6.0.0)
      actionpack (= 6.0.0)
      activejob (= 6.0.0)
      activerecord (= 6.0.0)
      marcel (~> 0.3.1)
    activesupport (6.0.0)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
      zeitwerk (~> 2.1, >= 2.1.8)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    bindex (0.8.1)
    bootsnap (1.4.4)
      msgpack (~> 1.0)
    builder (3.2.3)
    byebug (11.0.1)
    capybara (3.28.0)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (~> 1.5)
      xpath (~> 3.2)
    childprocess (1.0.0)
    concurrent-ruby (1.1.5)
    crass (1.0.5)
    erubi (1.9.0)
    ffi (1.11.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    i18n (1.7.0)
      concurrent-ruby (~> 1.0)
    jbuilder (2.9.1)
      activesupport (>= 4.2.0)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.3.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (0.9.2)
    mimemagic (0.3.3)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.12.2)
    msgpack (1.3.1)
    nio4r (2.5.2)
    nokogiri (1.10.4)
      mini_portile2 (~> 2.4.0)
    pg (1.1.4)
    public_suffix (4.0.1)
    puma (3.12.1)
    rack (2.0.7)
    rack-proxy (0.6.5)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (6.0.0)
      actioncable (= 6.0.0)
      actionmailbox (= 6.0.0)
      actionmailer (= 6.0.0)
      actionpack (= 6.0.0)
      actiontext (= 6.0.0)
      actionview (= 6.0.0)
      activejob (= 6.0.0)
      activemodel (= 6.0.0)
      activerecord (= 6.0.0)
      activestorage (= 6.0.0)
      activesupport (= 6.0.0)
      bundler (>= 1.3.0)
      railties (= 6.0.0)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (6.0.0)
      actionpack (= 6.0.0)
      activesupport (= 6.0.0)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.20.3, < 2.0)
    rake (13.0.0)
    rb-fsevent (0.10.3)
    rb-inotify (0.10.0)
      ffi (~> 1.0)
    regexp_parser (1.6.0)
    ruby_dep (1.5.0)
    rubyzip (1.3.0)
    sass (3.7.4)
      sass-listen (~> 4.0.0)
    sass-listen (4.0.0)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
    sass-rails (5.1.0)
      railties (>= 5.2.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    selenium-webdriver (3.142.4)
      childprocess (>= 0.5, < 3.0)
      rubyzip (~> 1.2, >= 1.2.2)
    spring (2.1.0)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (3.7.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    sqlite3 (1.4.1)
    thor (0.20.3)
    thread_safe (0.3.6)
    tilt (2.0.10)
    turbolinks (5.2.0)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    web-console (4.0.1)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (4.1.2)
      nokogiri (~> 1.6)
      rubyzip (~> 1.0)
      selenium-webdriver (>= 3.0, < 4.0)
    webpacker (4.0.7)
      activesupport (>= 4.2)
      rack-proxy (>= 0.6.1)
      railties (>= 4.2)
    websocket-driver (0.7.1)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.4)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.2.0)

PLATFORMS
  ruby

DEPENDENCIES
  bootsnap (= 1.4.4)
  byebug (= 11.0.1)
  capybara (= 3.28.0)
  jbuilder (= 2.9.1)
  listen (= 3.1.5)
  pg (= 1.1.4)
  puma (= 3.12.1)
  rails (= 6.0.0)
  sass-rails (= 5.1.0)
  selenium-webdriver (= 3.142.4)
  spring (= 2.1.0)
  spring-watcher-listen (= 2.0.1)
  sqlite3 (= 1.4.1)
  turbolinks (= 5.2.0)
  tzinfo-data
  web-console (= 4.0.1)
  webdrivers (= 4.1.2)
  webpacker (= 4.0.7)

RUBY VERSION
   ruby 2.6.3p62

BUNDLED WITH
   1.17.2

问题

我还必须做什么才能简单地部署和运行这个(一个地狱般的世界应用程序)?

编辑 1 - 我的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

我可以看到这里指的是 sqlite3 的生产数据库。我实际上没有使用它。只有一个动作说'hello world'......为什么它试图连接到数据库?!?!?!

标签: ruby-on-railsrubysqliteherokurubygems

解决方案


请注意您的生产数据库设置如何使用*default和引用 sqlite3?这就是导致错误的原因。该*default部分在默认设置下加载所有内容,即这部分

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

所以你需要更改下的设置production并删除那些东西。

production:
  adapter: postgresql

确保推送到 git 并部署到 heroku,以便更改生效。

如果您有需要运行的迁移,那么您还需要heroku run rake db:migrate从控制台运行以设置数据库表,它甚至会在“hello world”页面上查找此连接,因此需要为应用程序设置数据库工作(据我所知)。


推荐阅读