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
But this returns the following error:
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
I have tried following advice here but with RAILS_ENV=production
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.
Any advise would be greatly appreciated.
Edit: As advised I ran:
heroku pg:reset DATABASE
heroku run rake db:migrate
But then I received an error
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