Ruby-on-rails ActiveRecord::Base 没有连接池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38176304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No connection pool for ActiveRecord::Base
提问by Oscar Valdez Esquea
I'm trying to use rails 4.2.6 to develop an app. I'm trying to use postgres for database. Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error.
我正在尝试使用 rails 4.2.6 开发应用程序。我正在尝试将 postgres 用于数据库。服务器启动正常,但是当我尝试加载页面时,它会抛出此“ActiveRecord::Base 没有连接池”错误。
What could it be?
会是什么呢?
EDIT
编辑
The pg gem wasn't working properly. I had to comment it before starting the server and then uncomment it from my GemFile afterwards. I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended). I removed ruby 2.3 and set up everything under ruby 2.0 environment. It's now working properly.
pg gem 无法正常工作。我必须在启动服务器之前对其进行评论,然后在我的 GemFile 中取消评论它。我意识到我使用的是 Ruby 2.3 而不是 Ruby 2.0(如预期的那样)。我删除了 ruby 2.3 并在 ruby 2.0 环境下设置了所有内容。它现在工作正常。
I had read somewhere that there were some issues with the 'pg' gem in newer Rails releases, requiring people to use 'gem install pg --pre' instead for installing the gem. I tried that, but then my app was requiring the 'pg' gem in my GemFile and, well, the problem stated above showed up again.
我在某处读到过,在较新的 Rails 版本中,'pg' gem 存在一些问题,要求人们使用 'gem install pg --pre' 来安装 gem。我试过了,但后来我的应用程序需要我的 GemFile 中的“pg”gem,好吧,上面提到的问题又出现了。
This is how my database.yml file ended up:
这就是我的 database.yml 文件的最终结果:
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: -------
password: -------
pool: 5
development:
<<: *default
database: myDbName
回答by golfadas
If you are experiencing this error from a rake task, chances are you are not running the :environmenttask before your task.
如果您在执行 rake 任务时遇到此错误,很可能您没有在:environment执行任务之前运行该任务。
Changing:
改变:
task :task_name do
end
to:
到:
task task_name: :environment do
end
Should fix the issue.
应该解决问题。
回答by Promise Preston
This issue arises when the server cannot find the corresponding database it depends on to pull data from.
当服务器找不到它所依赖的相应数据库来提取数据时,就会出现此问题。
I had same issue from my end, I updated my version of Sqlite3, and it was higher than the version that the current Puma Server version supports.
我最后遇到了同样的问题,我更新了我的 Sqlite3 版本,它高于当前 Puma Server 版本支持的版本。
I simply had to uninstall the Sqlite3 version, and then install the version supported by the current version of Puma Server.
我只需要卸载Sqlite3版本,然后安装当前版本的Puma Server支持的版本。
gem uninstall sqlite3
This will uninstall the updated version of Sqlite3, and then you run the code below stating the version supported by your current server.
这将卸载 Sqlite3 的更新版本,然后运行下面的代码,说明当前服务器支持的版本。
gem install sqlite3
Or you can as well open your Gemfile, and then include the version for the database that you are using
或者你也可以打开你的 Gemfile,然后包含你正在使用的数据库的版本
gem 'sqlite3', '~> 1.3.6'
N/B:The sqlite3 version is the most current version as at the time of writing this answer
N/B:在撰写此答案时,sqlite3 版本是最新版本
And then run
然后运行
bundle update
to install the version of the database that you specified.
安装您指定的数据库版本。
That's all.
就这样。
I hope this helps.
我希望这有帮助。
回答by M. Karim
Check the database.ymlif all settings are ok. If its the first time and you didn't create the database yet use this command to create the database
检查database.yml所有设置是否正常。如果是第一次并且您还没有创建数据库,请使用此命令创建数据库
rake db:create
If the database already exists try reset the db migrations,
如果数据库已经存在,请尝试重置数据库迁移,
rake db:migrate:reset
Hope it'll solve the problem. Go to rails console and try something to check if its working or not.
希望它能解决问题。转到 rails 控制台并尝试检查其是否正常工作。
回答by thomasstephn
For PostgreSQL your database.ymlfile should look something like that:
对于 PostgreSQL,您的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: your_db_name
Also make sure that you have the gem installed: in your Gemfile:
还要确保您安装了 gem: 在您的 Gemfile 中:
gem 'pg'
Finally, restart your server.
最后,重启你的服务器。
Hope that helps
希望有帮助
Edit: I almost forgot, make sure you have your PostgresSQL running, check this linkfor download and setup.
编辑:我差点忘了,请确保您的 PostgresSQL 正在运行,请查看此链接以进行下载和设置。
回答by sathya_dev
I've encountered the same problem when I try to access the Model before creating the DataBase.
在创建数据库之前尝试访问模型时,我遇到了同样的问题。
Make sure you run rake db:migrateat least once.
确保您rake db:migrate至少运行 一次。
If you already run migration then check the Database as mentioned in previous answers.
如果您已经运行迁移,请检查之前答案中提到的数据库。
回答by Anand Raja
when you are running rails db:migratein data base rows will be created according to your migration files. you can see schema for further info.
当您rails db:migrate在数据库中运行时,将根据您的迁移文件创建行。您可以查看架构以获取更多信息。
回答by user9869932
In my case, the config/database.ymlwas taking variables from the environment:
就我而言,它config/database.yml从环境中获取变量:
# ...
test:
<<: *default
database: <%= ENV.fetch("DB_NAME") %>_test
username: <%= ENV.fetch("DB_USER") %>
password: <%= ENV.fetch("DB_PASS") %>
# ...
The error was coming when the new terminal window where I was running the bundle exec rails specdid not have those variables initialized.
当我运行的新终端窗口bundle exec rails spec没有初始化这些变量时,就会出现错误。
Doing,
正在做,
$ export DB_NAME=mydb
$ export DB_USER=myuser
$ export DB_PASS=mypass
$ bundle exec rails spec
fixed the issue.
解决了这个问题。
回答by The Whiz of Oz
I had to simply restart the server for the warning to go away.
我不得不简单地重新启动服务器以使警告消失。
回答by rodmclaughlin
Rails 5
导轨 5
New app
新应用
Using Postgresql for both test and development
使用 Postgresql 进行测试和开发
Specs run, so Rails can connect to Postgresql
规范运行,因此 Rails 可以连接到 Postgresql
But when I started the web app, I got "No connection pool with id primary found."
但是当我启动 Web 应用程序时,我收到“未找到 id 主要的连接池”。
Ran 'bin/rails db:migrate:reset' and restarted the app. It worked. No idea why.
运行 'bin/rails db:migrate:reset' 并重新启动应用程序。有效。不知道为什么。
database.yml:
数据库.yml:
development:
adapter: postgresql
encoding: unicode
database: rails5_development
pool: 5
username: foo
password: bar
host: localhost
port: 5432
test:
adapter: postgresql
encoding: unicode
database: rails5_test
pool: 5
username: foo
password: bar
host: localhost
port: 5432

