我在第一次部署Rails 4应用程序时在Heroku上成功运行rake db:migrate
:
$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.7124
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
Migrating to DeviseCreateUsers (20131223210005)
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
-> 0.0431s
-- add_index(:users, :email, {:unique=>true})
-> 0.0093s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0185s
== DeviseCreateUsers: migrated (0.0716s) =====================================
Migrating to AddNameToUsers (20131223210006)
== AddNameToUsers: migrating =================================================
-- add_column(:users, :name, :string)
-> 0.0060s
== AddNameToUsers: migrated (0.0062s) ========================================
Migrating to AddConfirmableToUsers (20131223210008)
== AddConfirmableToUsers: migrating ==========================================
-- add_column(:users, :confirmation_token, :string)
-> 0.0030s
-- add_column(:users, :confirmed_at, :datetime)
-> 0.0016s
-- add_column(:users, :confirmation_sent_at, :datetime)
-> 0.0018s
-- add_column(:users, :unconfirmed_email, :string)
-> 0.0016s
== AddConfirmableToUsers: migrated (0.0085s) =================================
Migrating to RolifyCreateRoles (20131223210011)
== RolifyCreateRoles: migrating ==============================================
-- create_table(:roles)
-> 0.0139s
-- create_table(:users_roles, {:id=>false})
-> 0.0021s
-- add_index(:roles, :name)
-> 0.0093s
-- add_index(:roles, [:name, :resource_type, :resource_id])
-> 0.0143s
-- add_index(:users_roles, [:user_id, :role_id])
-> 0.0095s
== RolifyCreateRoles: migrated (0.0502s) =====================================
现在,当我进入控制台 - heroku run console
并尝试访问任何模型时,我收到no database connection
消息。
$ heroku run console
Running `console` attached to terminal... up, run.9633
Loading production environment (Rails 4.0.2)
irb(main):001:0> User
=> User(no database connection)
irb(main):002:0> User
=> User(no database connection)
irb(main):003:0> exit
这是我的Gemfile的连接版本(为简洁起见):
source 'https://rubygems.org'
gem 'rails', '4.0.2'
gem 'sass-rails', '~> 4.0.0'
gem 'cancan'
gem 'devise'
gem 'pg'
gem 'rolify'
gem 'thin'
gem "rails_admin"
gem "friendly_id"
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
gem 'quiet_assets'
gem 'rails_layout'
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
gem 'sextant'
end
group :production do
gem "rails_12factor"
end
ruby '2.0.0'
我的database.yml
文件:
# PostgreSQL. Versions 8.2 and up are supported.
#
development:
adapter: postgresql
host: localhost
encoding: unicode
database: mydbname
pool: 5
username: myusername
password:
template: template0
test:
adapter: postgresql
host: localhost
encoding: unicode
database: my_test_db
pool: 5
username: myusername
password:
template: template0
production:
adapter: postgresql
host: localhost
encoding: unicode
database: myproductiondb
pool: 5
password:
template: template0
不确定发生了什么。
答案 0 :(得分:5)
no database connection
并不意味着连接失败。它只是意味着Rails还没有连接到数据库(可能因为没有必要)。
您可以通过尝试执行查询来查看连接是否有效。例如
User.all
并查看是否会引发任何错误。
为了查看属性,您可以在模型上明确触发inspect
User.inspect