多列索引的Rails迁移 - 如何命名索引?

时间:2012-10-01 13:52:14

标签: ruby-on-rails ruby migration

这会添加索引,但不允许我设置名称。我怎么能这样做?

class AddIndexEventScheduleidDayStarttime < ActiveRecord::Migration
  def up
    add_index(:events, [:schedule_id, :day , :start_time], {:name => "event_schedule_by_day_and_time_index"})
  end 

  def down
    remove_index(:events, {name: "event_schedule_by_day_and_time_index"})
  end 
end

$ rake db:migrate
==  AddIndexEventScheduleidDayStarttime: migrating ============================
-- add_index(:events, [:schedule_id, :day, :start_time], {:name=>"event_schedule_by_day_and_time_index"})
   -> 0.2210s
==  AddIndexEventScheduleidDayStarttime: migrated (0.2212s) ===================

1 个答案:

答案 0 :(得分:8)

如果您查看文档(http://api.rubyonrails.org/classes/ActiveRecord/Migration.html),您会看到add_index为add_index(table_name, column_names, options)

所以编写代码如下:

  def up
    add_index(:events,[:schedule_id, :day , :start_time],name: "event_schedule_by_day_and_time_index")
  end

应该是你想要的。选项位于列ID之后。