Best way to add_index to database
I think one extra migration fits well:
class AddIndexes < ActiveRecord::Migration def self.up add_index :prices, :user_id add_index :prices, :price end def self.down remove_index :prices, :user_id remove_index :prices, :price endend
Or you can use change
syntax with newer versions of rails, look at DonamiteIsTnt
comment for details:
class AddIndexes < ActiveRecord::Migration def change add_index :prices, :user_id add_index :prices, :price endend
Once an app is in production, the intent is that migrations will be applied once.
If you're still developing your app, you can always add them as you've noted followed by a rake db:migrate:reset
this will wipe your database and re-create it.
Otherwise, create a new migration rails g migration add_user_id_index
.
class AddUserIdIndex < ActiveRecord::Migration def self.up add_index :prices, :user_id end def self.down remove_index :prices, :user_id endend
FWIW, add_index :prices
doesn't make sense. Indexes are per-column, not per-table.
You can always manually create indexes by logging into your database.
CREATE INDEX prices__user_id__idx ON prices (user_id);
Simple solution:
- create a new migration
- add the indexes there (they don't need to be in the older migrations)
- run the migrations