Running rake db:migrate in Rails fails (table already exists) using mysql

Barry Michael Doyle picture Barry Michael Doyle · Nov 29, 2014 · Viewed 7.7k times · Source

I'm relatively new to developing on rails.

I'm trying to migrate a newly created table using the following command:

$ rake db:migrate

but I constantly get an error that has something to do with the table already existing. But whenever I try run my rails app I get a Migrations are pending error.

I don't mind deleting the table because I've only just started working on this app so I've deleted my previous migration and I'd run the db:migrate command and there'd be nothing to migrate. Then I would add a new model again using the following command:

$ rails generate model User name:string

That then gives me an expected output of:

    invoke  active_record
      create    db/migrate/20141129134103_create_users.rb
   identical    app/models/user.rb
      invoke    test_unit
   identical      test/models/user_test.rb
   identical      test/fixtures/users.yml

But after that I run:

$ rake db:migrate --trace

And I get this output:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== 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/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/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:367:in `log'
/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 `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:228: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/schema_statements.rb:205:in `create_table'
/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:460:in `create_table'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:649:in `block in method_missing'
/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/2.1.0/benchmark.rb:279:in `measure'
/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 `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:641:in `method_missing'
/home/barry/Documents/Programming/Rails Projects/homework/db/migrate/20141129134103_create_users.rb:3:in `change'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:595:in `exec_migration'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
/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/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `call'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `block in execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:165:in `invoke'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:156:in `invoke_task'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block (2 levels) in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:121:in `run_with_threads'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:106:in `top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:84:in `block in run'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:182:in `standard_exception_handling'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:79:in `run'
/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>'
ActiveRecord::StatementInvalid: 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/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/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:367:in `log'
/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 `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:228: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/schema_statements.rb:205:in `create_table'
/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:460:in `create_table'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:649:in `block in method_missing'
/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/2.1.0/benchmark.rb:279:in `measure'
/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 `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:641:in `method_missing'
/home/barry/Documents/Programming/Rails Projects/homework/db/migrate/20141129134103_create_users.rb:3:in `change'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:595:in `exec_migration'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
/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/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `call'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `block in execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:165:in `invoke'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:156:in `invoke_task'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block (2 levels) in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:121:in `run_with_threads'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:106:in `top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:84:in `block in run'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:182:in `standard_exception_handling'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:79:in `run'
/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>'
Mysql2::Error: Table 'users' already exists
/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/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/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:367:in `log'
/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 `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/mysql2_adapter.rb:228: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/schema_statements.rb:205:in `create_table'
/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:460:in `create_table'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:649:in `block in method_missing'
/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/2.1.0/benchmark.rb:279:in `measure'
/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 `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:641:in `method_missing'
/home/barry/Documents/Programming/Rails Projects/homework/db/migrate/20141129134103_create_users.rb:3:in `change'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:595:in `exec_migration'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
/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/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `call'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `block in execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:165:in `invoke'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:156:in `invoke_task'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block (2 levels) in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `each'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block in top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:121:in `run_with_threads'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:106:in `top_level'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:84:in `block in run'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:182:in `standard_exception_handling'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:79:in `run'
/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

As I said, after running my rails app I get the Migrations are pending error, and I can't seem to understand what's wrong. Let me know if you need any other files to help diagnose this issue. The one thing I'm doing different from tutorials is that I'm using mysql for the db instead of sqlite3.

Thank you in advance :)

Answer

Michael Durrant picture Michael Durrant · Nov 29, 2014

You are deleting the migration, in rails. However the actual mysql table itself it not being touched (dropped) and then most of your problems are stemming from the fact that the two get out of sync.

Your options generally in this situation are:

- `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.
  • The 'schema_migrations table tracks this stuff and so you can examine this too.

Going forward, don't just delete migrations without considering the actual database tables that exist. Use the rails down migrations and db reset to keep your mysql db in sync. At the end of the day it is the schema_migrations table that needs to be correct.