Rails忽略config / database.yml

时间:2018-06-13 22:57:01

标签: ruby-on-rails ruby

我使用Rails但不遵循config/database.yml中的设置。

使用config/database.yml$DATABASE_URL是我的Heroku应用数据库的网址)

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  adapter: sqlite3
  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
  url: <%= ENV['DATABASE_URL'] %>

我跑了rails g controller hoge,然后我遇到了错误:

/myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': 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). (Gem::LoadError)
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:185:in `spec'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:58:in `establish_connection'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `each'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:25:in `<top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:256:in `block in load_dependency'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:662:in `new_constants_in'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:256:in `load_dependency'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /myapp/app/models/application_record.rb:1:in `<top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:477:in `load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:477:in `block in load_file'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:662:in `new_constants_in'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:476:in `load_file'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:374:in `block in require_or_load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `block in load_interlock'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/concurrency/share_lock.rb:149:in `exclusive'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:11:in `loading'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `load_interlock'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:357:in `require_or_load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:511:in `load_missing_constant'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:202:in `const_missing'
    from /myapp/app/models/user.rb:1:in `<top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:477:in `load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:477:in `block in load_file'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:662:in `new_constants_in'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:476:in `load_file'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:374:in `block in require_or_load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `block in load_interlock'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/concurrency/share_lock.rb:149:in `exclusive'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies/interlock.rb:11:in `loading'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:36:in `load_interlock'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:357:in `require_or_load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:511:in `load_missing_constant'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:202:in `const_missing'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `const_get'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `block in constantize'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `each'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `inject'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `constantize'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:583:in `get'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:614:in `constantize'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:316:in `get'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:83:in `to'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:78:in `modules'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:95:in `routes'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:162:in `default_used_route'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/mapping.rb:72:in `initialize'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:346:in `new'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise.rb:346:in `add_mapping'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:243:in `block in devise_for'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `each'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `devise_for'
    from /myapp/config/routes.rb:2:in `block in <top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:436:in `instance_exec'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:436:in `eval_block'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:418:in `draw'
    from /myapp/config/routes.rb:1:in `<top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `block in load_paths'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `each'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:55:in `load_paths'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:18:in `reload!'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:41:in `block in updater'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/file_update_checker.rb:81:in `execute'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:42:in `updater'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/routes_reloader.rb:31:in `execute_if_updated'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/finisher.rb:128:in `block in <module:Finisher>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `instance_exec'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `run'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:59:in `block in run_initializers'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `each'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `call'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
    from /home/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:58:in `run_initializers'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:353:in `initialize!'
    from /myapp/config/environment.rb:5:in `<top (required)>'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:102:in `preload'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:153:in `serve'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
    from /myapp/vendor/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /home/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /home/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from -e:1:in `<main>'

在Gemfile中:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

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

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'

  gem 'pry-rails'
  gem 'pry-doc'
  gem 'pry-byebug'
  gem 'pry-stack_explorer'

  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'letter_opener'
  gem 'letter_opener_web'
end

group :production do
  gem 'pg', '~> 0.20'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'config'
gem 'bootstrap-sass'
gem 'kaminari'
gem 'jquery-rails'
gem 'gon'
gem 'faraday'
gem 'faraday_middleware'
gem 'devise'
gem 'devise-bootstrap-views'
gem 'data-confirm-modal'

我的ruby版本是v2.5.0,OS是macOS Sierra 10.12.6。

为什么适配器设置为postgresql,尽管sqlite3config/database.yml? 如何正确使用配置?

我想将开发环境迁移到虚拟盒子(以避免像这样的繁琐问题),但我担心,在盒子里,我也会遇到这样的错误。可以吗?

1 个答案:

答案 0 :(得分:1)

Rails正在使用环境变量中的连接详细信息,而不是配置文件。在设置数据库时,Rails使用 ENV['DATABASE_URL']config/database.yml,并且在大多数情况下环境变量具有优先级。

查看the docs,了解配置数据库的两种方式如何互动。

您的案例中最简单的解决方案可能是从开发计算机中删除DATABASE_URL变量。您不太可能需要在那里设置它,如果您不小心从开发中连接到生产数据库,可能会导致问题。

在指定开发数据库时,您还可以使用url:键而不是database:键,因为该键确实优先于环境变量。它看起来像是:

development:
  <<: *default
  url: sqlite:db/development.sqlite3

您也应该从adapter删除defaults,因为您在生产中没有使用sqlite,但正在合并该密钥。您拥有的内容可能会起作用(因为环境变量会覆盖文件中的设置),但这很令人困惑。理想情况下,您可能希望在开发和生产中使用相同的数据库,以最大限度地减少两种环境之间的差异。