Smallcat_small Bill Thayer 9 posts

While attempting to move the payment options to a database table I get migration errors. I’ve been trying to revert back using >rake db:migrate VERSION=6 but I get the same errors. Basically it says that the table ‘payment_types’ exists but I’m under the impression that >rake db:migrate VERSION=6 should remove that table.

This is a partial copy from my cmd window of my last attempt.
----------------------

C:\InstantRails-2.0-win\rails_apps\depot>ruby script/generate scaffold payment_type label:string value:string

exists  app/models/
exists  app/controllers/
exists  app/helpers/
exists  app/views/payment_types
exists  app/views/layouts/
exists  test/functional/
exists  test/unit/
identical  app/views/payment_types/index.html.erb
identical  app/views/payment_types/show.html.erb
identical  app/views/payment_types/new.html.erb
identical  app/views/payment_types/edit.html.erb
identical  app/views/layouts/payment_types.html.erb
identical  public/stylesheets/scaffold.css
dependency  model
exists    app/models/
exists    test/unit/
exists    test/fixtures/
skip    app/models/payment_type.rb
identical    test/unit/payment_type_test.rb
skip    test/fixtures/payment_types.yml
exists    db/migrate
create    db/migrate/007_create_payment_types.rb
identical  app/controllers/payment_types_controller.rb
identical  test/functional/payment_types_controller_test.rb
identical  app/helpers/payment_types_helper.rb
route  map.resources :payment_types

C:\InstantRails-2.0-win\rails_apps\depot>rake db:migrate
(in C:/InstantRails-2.0-win/rails_apps/depot)
7 CreatePaymentTypes: migrating ==========================================
—create_table(:payment_types)
rake aborted!
Mysql::Error: Table ‘payment_types’ already exists: CREATE TABLE `payment_types` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `label` varchar(255) DEF
AULT NULL, `value` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL) ENGINE=InnoDB

(See full trace by running task with—trace)

 
Generic-user-small James West 70 posts

It seems to me as though your version 6 migration is the migration that created the tables. If you want to remove the table then migrate to the version before that.

I tend to use the roll back migration rather than a specific version number but I guess it all depends on what you actually want to achieve.

Anyway I guess you should look at the migration files that you have and migrate to the one prior to the version 6.

I have not explored the migrate down options yet and this may well prove to be another alternative for achieving the same thing.

Hope this helps

James

 
Generic-user-small Steve Ross 3 posts

You need to make sure that the migration has

def self.down drop_table :payment_types end

 
Smallcat_small Bill Thayer 9 posts

Thank you James & Steve.

My bad. After going through my command history I see I had the syntax wrong. I typed:

depot>rake db:migrate version = 6

it should have been

depot>rake db:migrate VERSION=6

I guess the caps and no spaces matter for the argument VERSION=6.

My next post will be the output from my console after running >rake db:migrate—trace

Here is my 007_create_payment_types.rb file:


class CreatePaymentTypes < ActiveRecord::Migration
  def self.up
    create_table :payment_types do |t|
      t.string :label
      t.string :value

      t.timestamps
    end
  end

  def self.down
    drop_table :payment_types
  end
end

 
Smallcat_small Bill Thayer 9 posts

Here’s my cmd window output:


C:\InstantRails-2.0-win\rails_apps\depot>rake db:migrate VERSION=6
(in C:/InstantRails-2.0-win/rails_apps/depot)

C:\InstantRails-2.0-win\rails_apps\depot>rake db:migrate --trace
(in C:/InstantRails-2.0-win/rails_apps/depot)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== 7 CreatePaymentTypes: migrating ============================================
-- create_table(:payment_types)
rake aborted!
Mysql::Error: Table 'payment_types' already exists: CREATE TABLE `payment_types` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `label` varchar(255) DEF
AULT NULL, `value` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL) ENGINE=InnoDB
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:281:in `execute'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:104:in `create_table'

C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:416:in `create_table'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:285:in `send'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:285:in `method_missing'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:265:in `say_with_time'
C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:265:in `say_with_time'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:281:in `method_missing'
./db/migrate//007_create_payment_types.rb:3:in `up_without_benchmarks'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `send'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `migrate'
C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `migrate'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:348:in `migrate'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:in `each'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:in `migrate'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:307:in `up'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:298:in `migrate'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/tasks/databases.rake:85
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `call'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `execute'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `each'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `execute'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:508:in `invoke_with_call_chain'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `synchronize'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `invoke_with_call_chain'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:494:in `invoke'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1931:in `invoke_task'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `each'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1903:in `top_level'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1881:in `run'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1878:in `run'
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/bin/rake:31
C:/InstantRails-2.0-win/ruby/bin/rake:19:in `load'
C:/InstantRails-2.0-win/ruby/bin/rake:19

C:\InstantRails-2.0-win\rails_apps\depot>

 
Smallcat_small Bill Thayer 9 posts

I even tried going back to VERSION=5 but got the same result:



C:\InstantRails-2.0-win\rails_apps\depot>rake db:migrate VERSION=5
(in C:/InstantRails-2.0-win/rails_apps/depot)
== 6 CreateLineItems: reverting ===============================================
-- drop_table(:line_items)
   -> 0.0470s
== 6 CreateLineItems: reverted (0.0470s) ======================================

C:\InstantRails-2.0-win\rails_apps\depot>rake db:migrate
(in C:/InstantRails-2.0-win/rails_apps/depot)
== 6 CreateLineItems: migrating ===============================================
-- create_table(:line_items)
   -> 0.0470s
== 6 CreateLineItems: migrated (0.0470s) ======================================

== 7 CreatePaymentTypes: migrating ============================================
-- create_table(:payment_types)
rake aborted!
Mysql::Error: Table 'payment_types' already exists: CREATE TABLE `payment_types` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `label` varchar(255) DEF
AULT NULL, `value` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL) ENGINE=InnoDB

(See full trace by running task with --trace)

C:\InstantRails-2.0-win\rails_apps\depot>

 
Generic-user-small Steve Ross 3 posts

The version that creates :payment_types should also contain the drop definition migrate to a previous version (ie: if 5 has the create payment_types then migrate back to 4 while making sure that v5 has drop :payment_types defined) then migrate back to 6 or whatever one you want.

 
Generic-user-small Steve Ross 3 posts

make sure when you migrate to VERSION=5 that you see the drop :payment_types executing. If it isn’t then that is why the error is being thrown… the table already exists. You might have to drop the table manually for rails to sync back up with which version you are using of the app and what version the db is on.

 
Generic-user-small David Johnson 1 post

I have a feeling you are sitting a bit in limbo in the database, where your schema is at 6 but at some point previously the payment_types table was created. You never are getting to version 7 to have it actually drop the table when you revert. You have a couple options, bring up your database in the console and manually drop the table. You could reset the database with rake although you will lose any data and then rebuild your entire database from scratch through the migrations. Add the drop table to your 006 migration temporarily and then remove once the table is deleted.

9 posts, 4 voices