问题描述
我正在尝试将rails应用程序部署到在本地运行良好的heroku,但是到目前为止,我无法使用heroku迁移数据库(PG :: UndefinedTable:ERROR失败).为了对此进行修改,我尝试遵循建议运行:
I am trying to deploy my rails app to heroku which runs fine locally but I have so far been unable to migrate my database with heroku (fails with PG::UndefinedTable: ERROR). To amend this I have tried to follow suggestions to run:
heroku run rake db:reset --trace
但这将返回以下错误:
Running rake db:reset --trace on ⬢ parent-comms-app... up, run.6270
** Invoke db:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:check_protected_environments
D, [2016-07-28T13:10:17.703689 #3] DEBUG -- : ActiveRecord::SchemaMigration Load (1.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-07-28T13:10:17.722965 #3] DEBUG -- : (1.0ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", :environment]]
rake aborted!
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=production
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1256:in `last_stored_environment'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `block in execute'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `execute'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:209:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:207:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:207:in `invoke_prerequisites'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:186:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:209:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:207:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:207:in `invoke_prerequisites'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:186:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:173:in `invoke'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:152:in `invoke_task'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block in top_level'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:117:in `run_with_threads'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:102:in `top_level'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:80:in `block in run'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:178:in `standard_exception_handling'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:77:in `run'
/app/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.2.0/bin/rake:23:in `<main>'
Tasks: TOP => db:reset => db:drop => db:check_protected_environments
我尝试了以下建议此处但具有RAILS_ENV = production
I have tried following advice here but with RAILS_ENV=production
我的database.yml
My database.yml
development:
adapter: postgresql
encoding: unicode
database: parent_communication_development
pool: 5
username: Jack
password:
test:
adapter: postgresql
encoding: unicode
database: parent_communication_test
pool: 5
username: Jack
password:
production:
adapter: postgresql
encoding: unicode
database: parent_communication_production
pool: 5
username: Jack
password: [I have set a password here]
我注意到我以前不小心将生产数据库命名为与开发数据库相同的名称,但是此后我对此进行了更改,并在本地将数据库重置/尝试对其进行修改.
I noticed that I had accidentally previously called my production database the same name as my development database but I have since changed that and reset the database locally/tried to amend this.
任何建议将不胜感激.
按照我的建议,我跑了:
As advised I ran:
heroku pg:reset DATABASE
heroku run rake db:migrate
但是后来我收到了错误
PG::UndefinedTable: ERROR: relation "teachers" does not exist
: CREATE TABLE "english_grades" ("id" serial primary key, "teacher_id" integer, "student_id" integer, "title" character varying, "subcategory" character varying, "performance_grade" character varying, "feedback" character varying, "grade_date" date, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_7438078349"
FOREIGN KEY ("teacher_id")
REFERENCES "teachers" ("id")
, CONSTRAINT "fk_rails_023c49decc"
FOREIGN KEY ("student_id")
REFERENCES "students" ("id")
)
当前,我对该表的迁移是
Currently my migration for this table is
class CreateEnglishGrades < ActiveRecord::Migration[5.0]
def change
create_table :english_grades do |t|
t.references :teacher, references: :users
t.references :student, references: :users
t.string :title
t.string :subcategory
t.string :performance_grade
t.string :feedback
t.date :grade_date
t.timestamps
end
add_foreign_key :english_grades, :users, column: :teacher_id
add_foreign_key :english_grades, :users, column: :student_id
end
end
推荐答案
您可以尝试使用以下命令:
you can try with below command :
要删除数据库:
heroku pg:reset DATABASE
要重新创建没有任何内容的数据库:
To recreate the database with nothing in it:
heroku run rake db:migrate
希望,这会对您有所帮助.
Hope, this will help you.
这篇关于将Rails应用程序部署到heroku:在架构中找不到环境数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!