Resetting the Migration

$ bundle exec rake db:migrate

While working on Chapter 6 of the Michael Hartl Ruby on Rails tutorial, I kept getting a crazy long error. This is an example of that error:

== 20141129134103 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) auto_increment PRIMARY KEY, `name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/bin/rake:33:in `<top (required)>'
/home/barry/.rbenv/versions/2.1.2/bin/rake:23:in `load'
/home/barry/.rbenv/versions/2.1.2/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

The error was occurring because I had already created a table named Users. To solve this, I wanted to reset my database back to the original. I used

$ rake db:reset

and then ran

$ bundle exec rake db:migrate

and everything worked perfectly!


http://stackoverflow.com/questions/27202970/running-rake-dbmigrate-in-rails-fails-table-already-exists-using-mysql

rake db:migrate:down  # To have rails remove the table and keep in sync*

delete ('drop table users') the table in mysql and then re-run the migrations.

Use rake db:reset as indicated by jykim to drop the database.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s