uniq:唯一索引
给已有字段添加 rails generate migration add_index_to_table_name column_name:uniq
or
新增字段时增加索引 rails generate migration add_column_name_to_table_name column_name:string:uniq:index
生成
class AddIndexToModerators < ActiveRecord::Migration
def change
add_column :moderators, :username, :string
add_index :moderators, :username, unique: true
end
end