Rails 4使用多个drop_table向下迁移

时间:2014-06-18 10:24:36

标签: ruby-on-rails ruby-on-rails-4 rake database-migration

这是我的迁移文件

class CreateDatabaseStructure < ActiveRecord::Migration
  def up
    create_table "categories", force: true do |t|
      t.string   "name",        null: false
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
    end


    create_table "cities", force: true do |t|
      t.integer "country_id",            null: false
      t.integer "region_id",             null: false
      t.string  "name",                  null: false
      t.float   "Latitude",              null: false
      t.float   "Longitude",             null: false
      t.string  "TimeZone",   limit: 10, null: false
      t.integer "DmaId",      limit: 2
      t.string  "County",     limit: 25
      t.string  "Code",       limit: 4
    end

    create_table "countries", force: true do |t|
      t.string  "name",                           null: false
      t.string  "FIPS104",             limit: 2,  null: false
      t.string  "ISO2",                limit: 2,  null: false
      t.string  "ISO3",                limit: 3,  null: false
      t.string  "ISON",                limit: 4,  null: false
      t.string  "Internet",            limit: 2,  null: false
      t.string  "Capital",             limit: 25
      t.string  "MapReference",        limit: 50
      t.string  "NationalitySingular", limit: 35
      t.string  "NationalityPlural",   limit: 35
      t.string  "Currency",            limit: 30
      t.string  "CurrencyCode",        limit: 3
      t.integer "Population",          limit: 8
      t.string  "Title",               limit: 50
      t.string  "Comment"
    end

  end

  def down
    drop_table :categories, :countries
    #drop_table :venues
  end
end

当我运行命令rake db:migrate:down VERSION=20140618100702时,我收到以下错误。

== 20140618100702 CreateDatabaseStructure: reverting ==========================
-- drop_table(:categories, :countries)
rake aborted!
StandardError: An error has occurred:

no implicit conversion of Symbol into Integer/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:486:in `[]'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:486:in `drop_table'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:649:in `block in method_missing'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:621:in `block in say_with_time'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:621:in `say_with_time'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:641:in `method_missing'
/Users/harshamv/Sites/nomad/db/migrate/20140618100702_create_database_structure.rb:176:in `down'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:598:in `exec_migration'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:578:in `block in migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:577:in `migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:752:in `migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:1040:in `ddl_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:936:in `run'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:818:in `run'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/railties/databases.rake:79:in `block (3 levels) in <top (required)>'
/Users/harshamv/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/Users/harshamv/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
TypeError: no implicit conversion of Symbol into Integer
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:486:in `[]'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:486:in `drop_table'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:649:in `block in method_missing'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:621:in `block in say_with_time'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:621:in `say_with_time'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:641:in `method_missing'
/Users/harshamv/Sites/nomad/db/migrate/20140618100702_create_database_structure.rb:176:in `down'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:598:in `exec_migration'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:578:in `block in migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:577:in `migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:752:in `migrate'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:1040:in `ddl_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:936:in `run'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/migration.rb:818:in `run'
/Users/harshamv/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.1/lib/active_record/railties/databases.rake:79:in `block (3 levels) in <top (required)>'
/Users/harshamv/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/Users/harshamv/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate:down
(See full trace by running task with --trace)

1 个答案:

答案 0 :(得分:2)

您不能一次将多个表传递给drop_table方法,而是应该这样做:

def down
  drop_table :categories
  drop_table :countries
end

请参阅drop_table