0

I am new to rails. I made an error when generating a db migration. I was trying to add avatar to restaurants. However I instead tried to add avatar to users.

This is the wrong command I entered:

rails g migration add_avatar_to_user avatar:string

However users does not exist it should be restaurants, so it should have been:

 rails g migration add_avatar_to_restaurant avatar:string

I tried to do rake db:migrate and then realized my error I got:

SQLite3::SQLException: no such table: users: ALTER TABLE "users" ADD "avatar" varchar

I read some answers on stack overflow and they just said to run the correct generate migration and it will fix it. So I did but it did not.

So then I tried doing a rake db:rollback, which removed restaurants (This is the one I actually want to keep).

However I can see in my db/migrate folder I still have the migration where I created restaurant, the correct migration for adding avatar to restaurants and the the incorrect migration as per the picture below.

enter image description here

So before I mess it up any more I am wondering whether if I just delete the incorrect migration from the tree and run a rake db:migrate whether that will fix it or is there something else I should be doing.

Thanks

1 Answer 1

1

You can rollback to your correct migration using,

rake db:migrate VERSION=2012342112

Make sure you use the version no you want to rollback to. In your case that is _create_restaurants.rb

Now delete the not necessary migration. _add_avatar_to_users

Then finally run rake db:migrate

At any given point you can check your migration status using

rake db:migrate:status
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.