加载 'sqlite3' Active Record 适配器时出错。当我在 Heroku 中部署时

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

我在 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)

我尝试按照指南进行降级 SQLite3,但没有用,我还阅读了必须使用 Rails 5.2 的解决方案,但我现在是 6.0。是否有可能使其与 Rails 6 一起使用?

这是我的 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]

这是我的 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

Heroku 本身不支持 SQLite3,虽然我看到您在生产中添加了 pg gem,但您还需要配置 Rails 应用程序以加载其通过编辑 config/database.yml 连接到 PostgreSQL 从 PostgreSQL 数据库。

您在 database.ymldefault: 部分下将 sqlite3 指定为默认适配器:

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

这是导致您出错的原因,因为 sqlite3 未安装在生产环境中。

您应该遵循 Sebastian Palma 在评论中提供的 link。在那里,您会找到有关如何配置您的应用程序以在 Heroku 上使用 PostgreSQL 的讨论。

Heroku 本身不支持 SQLite3。

首先打开你的 Gemfile 并删除这一行:

gem 'sqlite3'

替换为这一行:

gem 'pg'

然后运行bundle install.

接下来您需要转换 config/database.yml。打开现有文件,它可能看起来像这样:

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

您需要更换适配器

adapter: sqlite3

对此:

adapter: postgresql

注意适配器名称是 postgresql 而不是 postgrespg。您还需要将 database: 更改为自定义名称。最终版本可能如下所示:

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

安装 pg gem 并迁移 config/database.yml 文件后,您将需要创建数据库和 运行 任何预先存在的迁移。

现在,当您使用 Rails 推送到 Heroku 时,将自动配置一个开发级 PostgreSQL 实例并将其连接到您的应用程序。如果您不使用 Rails,您可能需要通过 运行ning

手动添加 PostgreSQL 插件
heroku addons:create heroku-postgresql

您应该从您的应用程序中删除 SQLite,并在开发、测试和生产中使用 Postgres。 RDBMS 之间存在许多小的不兼容性,您不想在将应用程序投入生产时发现它们。

1。在本地系统上安装 Postgres。

如何操作取决于您的系统。 OS-X 和 Windows 有简单的安装程序。在 Linux 上,您将通过包管理器安装它。

2。删除 sqlite gem.

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

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

运行 bundle update 重新生成 Bundle.lock.

3。配置 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。提交并推送

我遇到了类似的问题,仅在生产中更改适配器对我有用,所以 database.yml 就像:

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

希望它对其他人有用。