mysql - Getting empty migration when II try and create a unique index in my rails project -
i’m using rails 4.2.3 mysql 5.5.37. want create unique constraint on table, involving multiple columns, ran …
rails generate migration add_index :user_objects, [:day, :object, :user], :unique => true however, produces file, db/migrate/20160203003708_add_index.rb, had nothing in …
class addindex < activerecord::migration def change end end so nothing happens when run “rake db:migrate”. i’m doing wrong in attempt create unique index across multiple columns , what’s right way on command line?
according migrations guide , migrations api docs generating migrations command line supports creating new tables , adding new columns. table needs created or column(s) added determined name of migration using part after _to_. e.g. running command like:
rails generate migration add_username_to_user_objects username:string:uniq would generate migration adds unique usernamecolumn (even if exists) user_objects table. now, if command supported adding indexes etc. syntax hard remember , fragile (because of bash etc) many prefer writing code file anyway :) :
# db/migrate/20160203003708_add_index.rb def change add_index :user_objects, [:day, :object, :user], unique: true end a suggestion: order of fields in unique index affects performance , other rules regarding sql. e.g. if user fk users table, putting first cover queries use user in where, order, group by etc (e.g. select ... user_objects user_id = ...) without needing separate index it. more important factor cardinality, "how unique each of day, object, userin user_objects?" put unique first.
Comments
Post a Comment