我能够通过删除一对护栏来获得Rails(3.1.3)迁移的功能索引!
# lib/functional_indexes.rb
module ActiveRecord
module ConnectionAdapters
module SchemaStatements
#disable quoting of index columns to allow functional indexes (e.g lower(full_name))
def quoted_columns_for_index(column_names, options = {})
column_names
end
def index_name_for_remove(table_name, options = {})
index_name = index_name(table_name, options)
# disable this error check -- it can't see functional indexes
#unless index_name_exists?(table_name, index_name, true)
# raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
#end
index_name
end
end
end
end
我不得不做出我自己的索引名,虽然:
class AddLowerCaseIndexes < ActiveRecord::Migration
def up
add_index :people, 'lower(full_name)', :name => "index_people_on_lower_full_name"
add_index :people, 'lower(company)', :name => "index_people_on_lower_company"
end
def down
remove_index :people, :name => "index_people_on_lower_full_name"
remove_index :people, :name => "index_people_on_lower_company"
end
end
(你可能不需要在你的索引列名引号,除非你正在做的事情疯狂喜欢把空格或奇怪的字符在其中。)
(试图回滚不存在索引时,你很可能与Postgres的错误消息的罚款。)
为了使这个迁移可逆的,只需添加到'down': 'remove_index:temporary_events,:名称=> “temporary_events_campaign_tinfoid_date_messagetype”' – 2017-01-28 07:43:41