3

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

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) ===================
4

1 回答 1

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 之后。

于 2012-10-01T14:06:02.707 回答