Rails控制台无法连接到数据库

时间:2021-09-23 03:01:01

In last days i update my OS X to Maverics. Today when i try to create new project like this:

在过去的几天里,我将我的OS X更新为Maverics。今天,当我尝试创建这样的新项目时:

rails new abc

there were many problems but i install xcode and now it's work. Right now i open rails console like this:

有很多问题,但我安装xcode,现在它的工作。现在我打开rails控制台,如下所示:

rails console

and then whatever i write i only see:

然后无论我写什么我只看到:

Loading development environment (Rails 4.0.1)
1.9.3p448 :001 > Link
=> Link(no database connection)

What is wrong? Mysql is running, database exist. When i do rake db:migrate everything works fine.

哪里不对? Mysql正在运行,数据库存在。当我做rake db:migrate一切正常。

5 个解决方案

#1


59  

The console probably does have a database connection but is reporting that it doesn't.

控制台可能确实有一个数据库连接,但报告它没有。

To see if that's true, make a query in the console.

要查看是否为真,请在控制台中进行查询。

Link.count

That fixed the false positive warning for me and a colleague.

这为我和一位同事修正了误报。

#2


27  

In the past, referencing an ActiveRecord model immediately after loading the console would return its column information. Rails now only returns this information if it has been explicitly coerced to connect to the database by a method like connection, first, all, count, etc.

过去,在加载控制台后立即引用ActiveRecord模型将返回其列信息。如果已经通过connection,first,all,count等方法显式强制连接到数据库,Rails现在只返回此信息。

You can restore the previous behavior by adding this to your config/application.rb

您可以通过将此添加到config / application.rb来恢复以前的行为

console do
  ActiveRecord::Base.connection
end

#3


19  

According to the issues #12804 on rails/rails.

根据rails / rails上的问题#12804。

The message just tells you that AR has not yet connected to the database and therefore does not know the column information. I think it's desired not to connect to the database unless we need to fetch data.

该消息只是告诉您AR尚未连接到数据库,因此不知道列信息。我认为除非我们需要获取数据,否则不希望连接到数据库。

For now you could use Model.connection to establish a connection.

现在,您可以使用Model.connection建立连接。

#4


4  

Try using reload! on the console and ensure that you have records in the specified model if not create records for the relations etc..

尝试使用重新加载!在控制台上,如果没有为关系等创建记录,请确保您在指定模型中有记录。

I had the same problem on ubuntu. I used reload! in rails console and added records in the database.

我在ubuntu上遇到了同样的问题。我用了重装!在rails控制台中并在数据库中添加记录。

#5


3  

I'm having the same issue with Rails 4.0.1. It's occurring on the Linux server I'm deploying to as well as my Mavericks development machine.

我在使用Rails 4.0.1时遇到了同样的问题。它发生在我正在部署的Linux服务器以及我的Mavericks开发机器上。

The server works, specs work, but the console doesn't have a database connection.

服务器工作,规格工作,但控制台没有数据库连接。

Reverting to Rails 4.0.0 fixes the issue with the console.

恢复到Rails 4.0.0修复了控制台的问题。

I haven't found any other mention of this issue. There's probably an issue with the changes for 4.0.1 and the Postgres adapter, maybe? (Are you using Postgres?)

我还没有找到任何其他提及这个问题。可能是4.0.1和Postgres适配器的更改有问题吗? (你在使用Postgres吗?)

#1


59  

The console probably does have a database connection but is reporting that it doesn't.

控制台可能确实有一个数据库连接,但报告它没有。

To see if that's true, make a query in the console.

要查看是否为真,请在控制台中进行查询。

Link.count

That fixed the false positive warning for me and a colleague.

这为我和一位同事修正了误报。

#2


27  

In the past, referencing an ActiveRecord model immediately after loading the console would return its column information. Rails now only returns this information if it has been explicitly coerced to connect to the database by a method like connection, first, all, count, etc.

过去,在加载控制台后立即引用ActiveRecord模型将返回其列信息。如果已经通过connection,first,all,count等方法显式强制连接到数据库,Rails现在只返回此信息。

You can restore the previous behavior by adding this to your config/application.rb

您可以通过将此添加到config / application.rb来恢复以前的行为

console do
  ActiveRecord::Base.connection
end

#3


19  

According to the issues #12804 on rails/rails.

根据rails / rails上的问题#12804。

The message just tells you that AR has not yet connected to the database and therefore does not know the column information. I think it's desired not to connect to the database unless we need to fetch data.

该消息只是告诉您AR尚未连接到数据库,因此不知道列信息。我认为除非我们需要获取数据,否则不希望连接到数据库。

For now you could use Model.connection to establish a connection.

现在,您可以使用Model.connection建立连接。

#4


4  

Try using reload! on the console and ensure that you have records in the specified model if not create records for the relations etc..

尝试使用重新加载!在控制台上,如果没有为关系等创建记录,请确保您在指定模型中有记录。

I had the same problem on ubuntu. I used reload! in rails console and added records in the database.

我在ubuntu上遇到了同样的问题。我用了重装!在rails控制台中并在数据库中添加记录。

#5


3  

I'm having the same issue with Rails 4.0.1. It's occurring on the Linux server I'm deploying to as well as my Mavericks development machine.

我在使用Rails 4.0.1时遇到了同样的问题。它发生在我正在部署的Linux服务器以及我的Mavericks开发机器上。

The server works, specs work, but the console doesn't have a database connection.

服务器工作,规格工作,但控制台没有数据库连接。

Reverting to Rails 4.0.0 fixes the issue with the console.

恢复到Rails 4.0.0修复了控制台的问题。

I haven't found any other mention of this issue. There's probably an issue with the changes for 4.0.1 and the Postgres adapter, maybe? (Are you using Postgres?)

我还没有找到任何其他提及这个问题。可能是4.0.1和Postgres适配器的更改有问题吗? (你在使用Postgres吗?)