【问题标题】:How can we use ActiveRecord and deploy to Heroku without using sqlite3?我们如何在不使用 sqlite3 的情况下使用 ActiveRecord 并部署到 Heroku?
【发布时间】:2015-12-21 06:06:34
【问题描述】:

我正在使用 ActiveRecord 类,并且默认使用“sqlite3”。 然后我想将它部署在 Heroku(它使用 PostgreSQL)上。所以我只是在 Gemfile 中添加了它并安装了依赖项。

当我尝试推送到 Heroku 时,它会抛出以下错误:

remote:        Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
remote:   
remote:        /tmp/build_12cfde1548d0f1ff19f95340a86aab63/vendor/ruby-2.0.0/bin/ruby extconf.rb
remote:        checking for sqlite3.h... no
remote:        sqlite3.h is missing. Try 'port install sqlite3 +universal',
remote:        'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
remote:        and check your shared library search path (the
remote:        location where your sqlite3 shared library is located).
remote:        *** extconf.rb failed ***

然后我尝试删除 sqlite3 gem,当我尝试运行它时,我从应用程序中收到了这个错误(我还没有尝试在 Heroku 上部署):

Error:[rake --tasks] /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': 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). (Gem::LoadError)
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/base.rb:315:in `<module:ActiveRecord>'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/base.rb:26:in `<top (required)>'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:319:in `active_record_configured?'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:251:in `disconnect_database'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:97:in `preload'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:143:in `serve'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:131:in `block in run'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:125:in `loop'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application.rb:125:in `run'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.4.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/adeesh/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

我的想法已经用完了,并且对 ruby​​ 或 rails 非常陌生。任何帮助表示赞赏!

 source 'https://rubygems.org'


 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '4.2.4'
 # Use SCSS for stylesheets
 gem 'sass-rails', ' 5.0.2'
 # Use Uglifier as compressor for JavaScript assets
 gem 'uglifier', '>= 1.3.0'
 # Use CoffeeScript for .coffee assets and views
 gem 'coffee-rails', '~> 4.1.0'
 # See https://github.com/rails/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby
 # Use jquery as the JavaScript library
 gem 'jquery-rails' , '>= 4.0.3'
 # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
 gem 'turbolinks', '2.3.0'
 # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
 gem 'jbuilder', '2.2.3'
 # bundle exec rake doc:rails generates the API under doc/api.
 gem 'sdoc', ' 0.4.0', group: :doc

 # Use ActiveModel has_secure_password
 gem 'bcrypt', '~> 3.1.7'

 # Use Unicorn as the app server
 # gem 'unicorn'

 # Use Capistrano for deployment
 # gem 'capistrano-rails', group: :development

 group :development, :test do
 # Use sqlite3 as the database for Active Record
   #gem 'sqlite3'
   #gem 'activerecord'

   gem 'pg'
   gem 'rails_12factor', '>= 0.0.2'
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
   gem 'byebug', '>= 3.4.0'

   # Access an IRB console on exception pages or by using <%= console %> in views
   gem 'web-console', '~> 2.0'

   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
   gem 'spring'

 end

 group :production do
   #This is for use with Heroku
   gem 'pg'
   gem 'rails_12factor', '>= 0.0.2'
 end

下面是我的 database.ml 文件:

# 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

更新 2:

如果我尝试在网页上运行开发(本地主机)数据库,则会收到此错误:

当我将它添加到开发和测试组时,我仍然得到这些错误:

remote:        
remote:        Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
remote:        
remote:        /tmp/build_23334fae90f17ca215e6f12340967cbd/vendor/ruby-2.0.0/bin/ruby extconf.rb
remote:        checking for sqlite3.h... no
remote:        sqlite3.h is missing. Try 'port install sqlite3 +universal',
remote:        'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
remote:        and check your shared library search path (the
remote:        location where your sqlite3 shared library is located).
remote:        *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
remote:        libraries and/or headers.  Check the mkmf.log file for more details.  You may
remote:        need configuration options.
remote:    
remote:        Provided configuration options:
remote:        --with-opt-dir
remote:        --without-opt-dir
remote:        --with-opt-include
remote:        --without-opt-include=${opt-dir}/include
remote:        --with-opt-lib
remote:        --without-opt-lib=${opt-dir}/lib
remote:        --with-make-prog
remote:        --without-make-prog
remote:        --srcdir=.
remote:        --curdir
remote:        --ruby=/tmp/build_23334fae90f17ca215e6f12340967cbd/vendor/ruby-2.0.0/bin/ruby
remote:        --with-sqlite3-dir
remote:        --without-sqlite3-dir
remote:        --with-sqlite3-include
remote:        --without-sqlite3-include=${sqlite3-dir}/include
remote:        --with-sqlite3-lib
remote:        --without-sqlite3-lib=${sqlite3-dir}/
remote:    
remote:    
remote:        Gem files will remain installed in /tmp/build_23334fae90f17ca215e6f12340967cbd/vendor/bundle/ruby/2.0.0/gems/sqlite3-1.3.10 for inspection.
remote:        Results logged to /tmp/build_23334fae90f17ca215e6f12340967cbd/vendor/bundle/ruby/2.0.0/gems/sqlite3-1.3.10/ext/sqlite3/gem_make.out
remote:        An error occurred while installing sqlite3 (1.3.10), and Bundler cannot
remote:        continue.
remote:        Make sure that `gem install sqlite3 -v '1.3.10'` succeeds before bundling.
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  ! 
remote:  !     Detected sqlite3 gem which is not supported on Heroku.

【问题讨论】:

标签: ruby-on-rails ruby sqlite activerecord heroku


【解决方案1】:

Heroku does not support SQLite。它默认支持 PostgreSQL(以及其他一些带有附加组件的数据库)。要在 Heroku 上使用 PostgreSQL,请打开 config/database.yml 并查找生产部分,如下所示:

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

将adapter: sqlite3更改为adapter: postgresql并修改数据库名称。结果应该是这样的:

production:
  adapter: postgresql
  database: db_production

您还需要将 PostgreSQL 适配器添加到您的 Gemfile:

gem 'pg', group: [:production, :staging]

我建议您在开发中也配置 PostgreSQL。 SQLite 不仅不如 PostgreSQL 强大,而且您还希望您的开发环境尽可能地匹配生产环境。

【讨论】:

  • 对不起,我忘了提。我也试过这样做。它再次抛出错误。我认为这与我有一个使用 ActiveRecord::Base 的类有关
  • 如果你不解释错误是什么,它很难帮助你。上面的代码确实有效,因为我使用它没有问题。此外,ActiveRecord::Base 是您所有模型的继承来源
  • 当我尝试在本地机器上运行开发时收到此错误。 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).
  • 照它说的做...将gem 'sqlite3' 添加到您的Gemfile,但将其添加到组中以进行开发,例如gem 'sqlite3', group: :development
  • 是的。我确实尝试过。但是当我这样做时,我会得到remote: Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /tmp/build_23334fae90f17ca215e6f12340967cbd/vendor/ruby-2.0.0/bin/ruby extconf.rb remote: checking for sqlite3.h... no remote: sqlite3.h is missing. Try 'port install sqlite3 +universal', remote: 'yum install sqlite-devel' or 'apt-get install libsqlite3-dev' remote: and check your shared library search path (the remote: location where your sqlite3 shared library is located). remote: *** extconf.rb failed
【解决方案2】:

来自 mu 的评论是正确的:与 ActiveRecord 的差异 - 您可能必须返回并对某些事情进行查询更改(这就是我最终不得不做的事情!)。

但是,如果您现在只想推送到 heroku 并且仍然能够在本地运行它以进行测试,那么当您想在 gemfile 中包含 sqlite 时,您应该能够添加 group: :development (如下所示)。这样,heroku 在您部署时会忽略它。如果这不起作用,可以发布您的 database.yml 文件和 gemfile,以便我们获得更多信息

gem 'sqlite3',组::开发

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2014-06-07
    • 2014-10-20
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多