向表中新增唯一列
要向 users
新增新的唯一列 email
,請執行以下命令:
rails generate migration AddEmailToUsers email:string:uniq
這將建立以下遷移:
class AddEmailToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :email, :string
add_index :users, :email, unique: true
end
end