我是新的Ruby on Rails。我试图在现有项目的多个列上添加索引。在互联网上,我看到了可以如下添加的示例。但是我不明白如何生成迁移脚本以添加索引。任何帮助将不胜感激。
add_index :facility_user_assignments, [:facility_id, :user_id], unique: true.
以下是我在互联网上找到的内容。如何更改为添加到多列]
rails generate migration AddIndexToPhoneToUsers phone:string:uniq
提前感谢。
大多数迁移无法通过CLI生成。相反,您应该只生成一个空迁移,然后手动填写change
方法。
rails generate migration AddIndexToPhoneToUsers
用途:
rails generate migration add_indices_to_phone phone:string:uniq
然后转到您的迁移文件'add_indices_to_phone.rb',并在迁移前添加所需的字段。
class AddIndicesToPhone < ActiveRecord::Migration
def change
add_column :phone, :string
add_column :another_field, :string # add these fields manually
end
add_index :phone, :phone, unique: true
add_index :phone, :another_field, unique: true # add these fields manually
end
我希望这是您想要的。