我在 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一起使用?
这是我的宝石文件:
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
你应该从你的应用程序中删除SQLite,并在开发、测试和生产中使用Postgres。RDBMS之间存在许多小的不兼容性,当您将应用程序推向生产环境时,您不希望发现它们。
1. 在本地系统上安装 Postgres。
如何执行此操作取决于您的系统。OS-X和Windows有简单的安装程序。在 Linux 上,您可以通过包管理器安装它。
2. 去除 sqlite 宝石。
# 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:
部分下指定sqlite3
作为默认适配器:
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
这会导致您的错误,因为sqlite3
未安装在生产环境中。
您应该按照塞巴斯蒂安·帕尔马在评论中提供的链接进行操作。在那里,您将找到有关如何配置应用程序以在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
而不是postgres
或pg
。您还需要将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,你可能需要通过运行来手动添加PostgreSQL插件
。heroku addons:create heroku-postgresql
Heroku本身不支持SQLite3,虽然我看到你在生产中添加了pg
gem,但你还需要配置你的Rails应用程序,通过编辑config/database.yml
从PostgreSQL加载其数据库连接到PostgreSQL。
我遇到了类似的问题,仅在生产中更改适配器对我有用,所以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
希望它对其他人有用。