Ruby rails 生成迁移命令以在 Ruby 的多个列上添加索引 Rails

Ruby rails generate migration commond to add Index on multiple columns in Ruby on Rails

我是 Rails 的新 Ruby。我试图在现有项目的多个列上添加索引。在互联网上,我看到了可以添加的示例,如下所示。但我无法理解如何生成迁移脚本来添加索引。任何帮助将不胜感激。

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 addIndicesToPhone 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

希望这就是您要找的。