Rails Migration Error on Git Project - ruby-on-rails

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.

Related

Rails tests not running due to apparently pending migrations

I have a very strange problem in which running rails test seems to be dropping a table in my test database. I can drop, create and migrate a database, then use psql to check the correct tables are there. If I then run rails t however, I am told ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "groups" does not exist, and checking with psql shows that the table is indeed now missing. Rails then tells me I have 24 pending migrations, which is all my migrations.
I fixed with
rails db:setup --trace

Rails: Migration Errors

when I go to my http://localhost:3000/ I am getting the following:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
Extracted source:
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
end
def load_schema_if_pending!
Also, when I tried to to the heroku run rake db:migrate in the console, it said:
StandardError: An error has occurred, this and all later migrations
canceled: PG::DuplicateColumn: ERROR: column "email" of relation
"users" already exists
I am new to ruby and followed the devise tutorial by Mackenzie Child. It's my last step to complete my first ruby application.
I am excited and looking forward to your help! :)
In your console run rake db:migrate
Make sure you in the project directory
You used devise generator to prepare migration for your User model. Your model was already in place before and already had an email column. Devise-generated migration tries to create the same column and, expectedly, fails, that's the reason for the error you're seeing:
PG::DuplicateColumn: ERROR: column "email" of relation "users" already exists
To fix that just open your devise-generated migration and remove the line which looks something like this:
t.string :email...
Then run rake db:migrate.
UPDATE
Since your database seems to be out of sync with your migrations it might be advisable to recreate it from scratch.
Run
rake db:drop db:create db:migrate
Note that all database data will be destroyed.

Heroku run rake db:migrate not working

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!

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

Resources