ruby ActiveRecord::AdapterNotSpecified数据库配置未指定适配器

7jmck4yq  于 2023-04-29  发布在  Ruby
关注(0)|答案(8)|浏览(120)

当我使用heroku打开我的web应用程序工作正常,但当我使用rails s(localhost)时,我遇到了这个错误:

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

这是为什么呢?
这是我的数据库。YML

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

这是我的gemfile:

source 'https://rubygems.org'

gem 'pg'

gem 'bootstrap-sass', '~> 3.1.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

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

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

# Use debugger
# gem 'debugger', group: [:development, :test]
ylamdve6

ylamdve61#

要使您的应用在本地工作,您需要:

  1. Install Postgresql在您的计算机上
  2. Create a database,满足您的开发需求(我们称之为my_app_development
    1.将database.yml更改为:
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: my_app_development

1.运行rake db:migrate

bvjxkvbb

bvjxkvbb2#

您没有显示导致此查询的命令,但如果传递的是字符串而不是符号,则可能会发生这种情况。
例如:

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}")
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

但是如果你使用一个符号,它就会起作用。

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}".to_sym)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007f2f484a32a0 #....
kxeu7u2r

kxeu7u2r3#

你的数据库。yml应该看起来像这样:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
  username: my_username
  password: my_password

development:
  <<: *default
  database: "development_database_name"

test:
  <<: *default
  database: "test_database_name"

production:
  <<: *default
  database: "production_database_name"

将development_database_name编辑为本地数据库名称。同时将my_username和my_password编辑为正确的数据库用户名和密码。

noj0wjuj

noj0wjuj4#

删除标签没有更多,识别完美,如:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  host: 192.168.0.121
  username: postgres
  password: passpostgres

development:
  <<: *default
  database: DBPOSTGRES
 # 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: DBPOSTGRES
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: DBPOSTGRES
  password: <%= ENV['passpostgres'] %>
uqzxnwby

uqzxnwby5#

如果你试图使用activerecord而不使用rails,你可能会遇到这个数据库问题。yml与多个环境设置。所以你需要像这样把环境键传递到配置设置中:

DB_ENV ||= 'development'
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details[DB_ENV])
nwlqm0z1

nwlqm0z16#

为什么在database.yml中使用yml node reference
你应该有这样的东西:

#config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: ****
  pool: 5
  username: ****
  password: ****
  host: ***.***.***.*** #-> only for third party db server

production:
  adapter: postgresql
  encoding: utf8
  database: ****
  pool: 5
  username: ****
  password: ****
  host: ***.***.***.*** #-> only for third party db server

更新

Rails使用数据库运行。您必须连接到数据库才能使其工作,为此,您必须在database.yml中定义不同的连接细节。
要定义正确的信息,您需要了解Rails在几种environments中运行-developmentproduction是最常用的两种
要让Rails在本地(开发)环境中工作,您需要定义正确的数据库细节。这意味着您需要连接到一个数据库-通常是通过setting up a local mysql / pgsql server完成的
底线是你连接到一个数据库使用:

  • 主机名
  • 用户名
  • 口令
  • 数据库名

您需要在config/database.yml文件中定义这些
如果在本地环境中运行服务器,则数据库。yml文件看起来像这样:

#config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: db_name
  pool: 5
  username: username
  password: password
zyfwsgd6

zyfwsgd67#

在我的情况下,原因在我的Rakefile中。
当我运行rake db:migrate时,我得到了这个:

rake db:migrate
rake aborted!
ActiveRecord::AdapterNotSpecified: The `default_env` database is not configured for the `default_env` environment.

Available databases configurations are:

development
test
production

我在Rakefile中找到了这一行:
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
并更改为默认值:ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/db_name')
现在一切正常详细信息here

5n0oy7gb

5n0oy7gb8#

如果您尝试使用Active Record而不使用Rails(例如,如果您使用Sinatra,并且您正在使用sinatra-activerecord),请将以下内容添加到Rakefile

require 'sinatra/activerecord'

相关问题