我在 Heroku 上的应用程序遇到问题,为了确定它是特定于我的生产环境还是特定于 Heroku 本身,我在我的计算机上使用 Rails s -e production 在本地运行我的生产环境
当我尝试加载帖子索引时,出现以下错误:
PG::UndefinedTable: ERROR: relation "posts" does not exist
LINE 1: SELECT "posts".* FROM "posts"
^
: SELECT "posts".* FROM "posts"
PG::UndefinedTable: ERROR: relation "posts" does not exist
LINE 1: SELECT "posts".* FROM "posts"
奇怪的是我的帖子索引在 Heroku 上以及开发和测试中都运行良好。当我运行 bundle exec rake db:create:all 时,我得到以下输出:
project_development already exists
project_test already exists
project_production already exists
我的架构如下:
create_table "posts", force: true do |t|
t.string "title"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "author_id"
t.boolean "published"
end
当我在命令行中启动rails db时,我可以在运行\dt时看到预期的表:
public | posts | table | Thomas
但是,当我使用 ENV=Production 启动它并运行\dt 时,我得到以下结果:
Schema | Name | Type | Owner
--------+-------------------+-------+--------
public | schema_migrations | table | Thomas
我尝试运行 ENV=生产 rake db:schema:load 但这并不能解决问题。
作为引用,这是我的database.yaml:
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test:
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
最佳答案
使用rake db:create:all
从database.yml
文件创建的生产
数据库是在您的计算机上本地创建的,而不是在赫罗库。
Heroku 会自动推断生产环境的数据库配置,并在您第一次运行 git push heroku master 时创建它。
关于posts
表不存在的错误消息是因为没有在heroku上运行迁移。运行 heroku run rake db:migrate
将迁移应用到 heroku 上的生产数据库。
此外,您不需要在本地环境中拥有 database.yml
的 生产
部分。
请参阅https://devcenter.heroku.com/articles/getting-started-with-rails4第一次使用 Heroku 时;它非常全面并且非常有帮助。
编辑
实际的问题是让本地生产数据库正常工作。
解决方案是使用 rake db:migrate RAILS_ENV=product
正确应用迁移
关于ruby-on-rails - 错误: Relation does not exist when running Rails in production using postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831470/