0

I tried to create a migration to add roles to my user tables but i accidentally typed AddRolesToUsers instead of AddRoleToUser. So i tried creating a new migration with the correct AddRoleToUsers but when i tried to run rake db:migrate i got an error :

SQLite3::SQLException: duplicate column name: role: ALTER TABLE "users" ADD "role" integer/Users/miguel/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize' 

I tried rake db:migrate:down VERSION= do delete the one I had to type on but i keep getting the same error . PS: i deleted the migration file manually after running rake db:migrate:down VERSION=

rails g migration AddRoleToUsers role:integer

migration file :

class AddRoleToUsers < ActiveRecord::Migration def change add_column :users, :role, :integer end end 
4
  • Can you display the content of the two migration files? Commented Jan 24, 2016 at 7:58
  • Have you run any migrations yet, either the typo one or the new one? I'm basically asking if the :role column has been added to the users table yet or if the migration has just been created Commented Jan 24, 2016 at 7:59
  • @RuNpiXelruN i cant run a migrate i keep getting that error Commented Jan 24, 2016 at 8:01
  • class AddRoleToUsers < ActiveRecord::Migration def change add_column :users, :role, :integer end end Commented Jan 24, 2016 at 8:02

2 Answers 2

1

When you ran the first migration, the role column was added to the Users table. The error on the second migration tells you that much.

To clear the migration pending error, you need to comment out the add_column statement in the new migration. i.e,

class AddRoleToUsers < ActiveRecord::Migration def change # add_column :users, :role, :integer end end 

Then run the migration. This way, the new migration should run successfully.

You can now uncomment it and delete the previous migration, so that when you deploy, only the newer migration is run and the role column is added successfully.

Sign up to request clarification or add additional context in comments.

1 Comment

@MiguelAngelQuintana You could tick it as answer if it worked, so you'd better guide others. You're welcome. :)
0

In that case right click and delete both migration files and start again. The error exists because it thinks you want to add two columns both named role to your users table

UPDATE

In that case, if role already exists in your users table, a migration must've been successfully run and there is no need to run another. As long as the role column is there, trying to add another column called role will always give you an error. If you wanted to test that you are still able to add new columns you can always check by creating a test migration AddSomethingToUsers something:string and rake db:migrate to test, then rake db:rollback to undo..but it all seems like it's worked so I probably my wouldn't mess with it too much.

3 Comments

I have done that. First one i had the typo on so i deleted it made another one i got the error once i tried to run rake db:migrate and then i tried doing a third one after deleted the first two and the issue is still there.
Hmm..have a look at your schema..does the role column already exist in the users table? Sorry if you've already checked all this, just trying to rule things out
the thing is i cant continue doing my Rspec testing cause i get an error saying /Users/miguel/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:392:in check_pending!': (ActiveRecord::PendingMigrationError) Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=test`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.