Heroku run rake db:migrate not working - ruby-on-rails

Help!!
I am trying to push my production code to heroku but for some reason heroku run rake db:migrate command doesn't work. It gives me this error:
Migrating to RemoveMealPlanFromOrders (20150125085531)
== 20150125085531 RemoveMealPlanFromOrders: migrating =========================
-- remove_reference(:orders, :mealplan, {:index=>true})
PG::Error: ERROR: column "mealplan_id" of relation "orders" does not exist
: ALTER TABLE "orders" DROP "mealplan_id"
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: column "mealplan_id" of relation "orders" does not exist
: ALTER TABLE "orders" DROP "mealplan_id"/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `exec'
...
I have desperately tried everything including manually deleting the migration file the error references, running rake db:reset, and resetting heroku's database and even restarting the heroku app, but to no avail. I would infinitely appreciate if anyone could offer a solution to this error. I need to meet a deadline to submit this production code.
Thanks in advance!!!

Heroku Postgres is the SQL database service run by Heroku that is provisioned and managed.before pushing code-repository from git to Heroku you need to migrate database by following command.
heroku run rake db:migrate --app your_app_name.
after db-migaration.
git push heroku master
if you have done any changes to database make sure of migrating database before pushing into heroku.

I figured out the problem. Apparently I had removed the conflicting migration on my local machine but forgot to run the command git push heroku. Once I did that I was able to do heroku run rake db:migrate without a problem.
Happy coding!

Related

Rails Migration Error on Git Project

I am learning Ruby on Rails, an am getting an error creating the database. I run the following command from console:
rake db:create db:migrate db:seed
And get:
== 20140328232600 AddAuthLevelToUser: migrating ===============================
-- add_column(:users, :auth_level, :Integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
I looked at the error message on the webpage, and tried running:
'bin/rake db:migrate RAILS_ENV=development'
As suggested, but had little luck.
The project I am working on had been started by another team of developers, so I pulled it off git... Any suggestions?
Cheers
As per the error, you are running the migration AddAuthLevelToUser when you don't even have users table in database.
Firstly, verify if you have migration for users table, if not then create one.
If yes, then check that migration for users table has a VERSION NUMBER lower than that of AddAuthLevelToUser. Fix it and run the migrations.
Try running just rake db:create at first. What database is your project using? Work this out and use the database client to connect to the database on your local system, and verify whether the users table exists on the database. It's possible that the db:create job is not correctly creating all the tables necessary for the database schema.

Migration Errors on Heroku

I have a Rails app which I have recently pushed up to Heroku. When I try to migrate the database with:
heroku run rake db:migrate
I am getting the rake aborted! message and complains about a table that doesn't exist.
Locally, when I run the migrate command there are no issues so why is Heroku different?
I did experience this issue the last time I migrated on Heroku too - I ended up using:
heroku rake db:schema:load
to overcome the problem but I really want to get to the root of the problem so it stops happening.
Any suggestions?
EDIT: I know which migration file is at fault but is it safe to remove a file from the migrations folder?

Error with heroku run rake db:migrate

I am trying to run the command on Heroku
Heroku run rake db:migrate
but I get the error:
Migrating to AddNameToUsers (20130320002032)
== AddNameToUsers: migrating =================================================
-- add_column(:users, :name, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD COLUMN "name" character varying(255)
This might have to do with the fact that I had some issues with the migration files on my local server. I managed to work around it and had to delete one of the files, but I worry that I might have deleted something I need that hadn't been migrated to heroku's database?
my github for the account is https://github.com/jeremybelcher/omrails
Any help is appreciated
Your previous migrations are missing.
You can do:
rake db:create
rake db:schema:load
rake db:migrate
Which will recreate your database based on your schema.rb file.

Editing migrations after deployment to production

I'm finishing a major refactoring of an app and I am trying to clean up the migrations. I've made the changes and everything works great locally after reseting and re-migrating the database.
On production I can't get the migrations to run. I've tried every combination of reset, drop, etc. that I can think of but I keep getting this error.
It seems like the production database isn't being reset which is causing the migration to break. Any tips would be appreciated.
== AddFieldsToUsers: migrating ===============================================
-- add_column(:users, :company, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: company: ALTER TABLE "users" ADD "company" varchar(255)
For resetting the database you can drop and migrate the database again in production mode.
rake db:drop db:create db:migrate RAILS_ENV=production
And, if you want to edit some particular migration, you can reset it using its version no.
rake db:migrate:down VERSION=<version no.> RAILS_ENV=production
Edit the migration file, and then
rake db:migrate:up VERSION=<version no.> RAILS_ENV=production

Successful heroku run rake db:migrate but columns don't seem to work

I added a new column called level to my table called ClassRequest, ran a rake db:migrate on localhost and the view worked perfectly. Pushed the changes to heroku and the view met an error "NoMethodError: undefined method `level' for #"
So I ran heroku run rake db:migrate and that appears to be successful, showing the codes below.
Running rake db:migrate attached to terminal... up, run.1
== AddDetailsToClassRequests: migrating ======================================
-- add_column(:class_requests, :level, :string)
-> 0.0684s
== AddDetailsToClassRequests: migrated (0.0713s) =============================
But when I load the associated view, I still have this error "NoMethodError: undefined method `level' for #".
I'm new to rails so I don't know what's wrong, nor how do I know how to check what may have gone wrong.
heroku restart
Check the heroku's docs: https://devcenter.heroku.com/articles/rake
After running a migration you’ll want to restart your app with heroku
restart to reload the schema and pickup any schema changes.
You need to restart you app after you've run the migration. Try
heroku restart
and see if things improve.

Resources