rake db:migrate之后表已经存在

时间:2014-07-29 17:49:52

标签: ruby-on-rails

我跑了rake db:migrate,我收到以下错误:

Mysql2::Error: Table 'sdk_wizard_sessions' already exists: CREATE TABLE `sdk_wizard_sessions` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `using_mediation` tinyint(1) DEFAULT 0 NOT NULL, `user_id` int(11) NOT NULL, `selected_networks` text NOT NULL, `selected_games` text NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/Users/me/.rvm/gems/ruby-1.9.3-p547@global/gems/activerecord-3.2.15/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'

我看过stackoverflow,似乎没有人有一个很好的解决方案。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

该表已存在....要么删除表

rails g migration DropSdkWizardSession

并在创建的迁移中输入以下代码

class DropSdkWizardSession < ActiveRecord::Migration
  def change
    drop_table :sdk_wizard_sessions
  end
end

或者您可以更改当前表,使其看起来像您要创建的表(我认为这三个选项中的更好)

rails g migration ChangeSdkWizardSession

并将此代码添加到创建的迁移

class ChangeSdkWizardSession < ActiveRecord::Migration
  change_table :sdk_wizard_sessions do |t|
    t.remove :description, :name  #this will remove columns named :description or :name
    t.string :part_number         #this will add column of type string, called :part_number
    t.index :part_number          #this will add an index for column :part_number
    t.rename :upccode, :upc_code  #this would rename column :upccode, to :upc_code
  end
end

或者您可以创建一个新表并将其命名为不同的(懒惰选项)