我有一個表'用戶'列'電子郵件'。它曾經是唯一的(有索引),但新的要求是允許nils在那裏。是否有可能在rails 3遷移中更改列索引?
難道還有比一個更好的解決方案:
remove_index :users, :email
add_index :users, :email
?
最初人們用選項是唯一的說:
add_index :users, :email, :unique => true
我有一個表'用戶'列'電子郵件'。它曾經是唯一的(有索引),但新的要求是允許nils在那裏。是否有可能在rails 3遷移中更改列索引?
難道還有比一個更好的解決方案:
remove_index :users, :email
add_index :users, :email
?
最初人們用選項是唯一的說:
add_index :users, :email, :unique => true
我要說的是,你必須有正確的解決方案,該指數將需要再生,因此,爲什麼沒有update_index
。
嘿,這裏是我剛剛寫的一個遷移工作得很好。我有一個varchar(255)'enclosureUrl'列的'scraped_episodes'表。我需要使這個更長的網址,所以這是我使用的(Rails 3.2.13)
class ExpandEnclosureUrl < ActiveRecord::Migration
def up
# remove index cuz we need to
remove_index :scraped_episodes, :enclosureUrl
# change length to 2048 characters
change_column :scraped_episodes, :enclosureUrl, :text, :limit=>2048
# redo this index to only index the first 255 chars
add_index :scraped_episodes, :enclosureUrl, :length => 255
end
def down
# remove index cuz we need to
remove_index :scraped_episodes, :enclosureUrl
# use the same settings at when i first created this field
change_column :scraped_episodes, :enclosureUrl, :string, :limit=>nil
# use the same settings as when i first added this index
add_index :scraped_episodes, :enclosureUrl
end
end