Shambhu
Shambhu

Reputation: 71

how to create migration to add unique constrain scope by some columns

How do I add database constrain for following statement written in rails model

validates :column_1, uniqueness: { scope: [:column_2, :column_3] }

Thank in advance.

Upvotes: 3

Views: 2254

Answers (1)

Othmane El Kesri
Othmane El Kesri

Reputation: 603

Run the following migration:

def change    
  add_index :table_name, [:column_1, :column_2,:column_3], unique: true
end

Upvotes: 3

Related Questions