class AddIndexesToStyleFeatures < ActiveRecord::Migration def self.up add_index :stylefeatures , [:style_id , :feature_id] , :unique => true add_index :features , :name # check your data before making this unique end def self.down drop_index :features , :name drop_index :stylefeatures, [:style_id , :feature_id] end end
You might want to make the: name index of the: features class unique, but beware of this catch:
If you have entries that may contain NULL / nil fields that are part of an index, then do not use unique indexes. => check your details first
If during the removal of functions it may happen that the StyleFeatures element gets a reference to nil (instead of being deleted at all), then the presence of a unique index will also cause problems for this table.
See: Restricting Rails Uniqueness and Matching a Unique Db Index for a Zero Column
and: How to create a unique index in a NULL column?
Tilo source share