Forum: Ruby on Rails Migrations add_index

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-23 22:22
(Received via mailing list)
Hi all,

If I have the following migration:

class CreateDocuments < ActiveRecord::Migration
  def self.up
    create_table :documents do |t|
      t.column :created_at, :datetime
      t.column :identifier, :string
      t.column :file_format, :string
    end

    add_index :documents, :identifier, :unique => true
  end

  def self.down
    remove_index :documents, :identifier
    drop_table :documents
  end
end

Is it necessary (or recommended) to include the remove_index line in
the self.down method?  I realize that dropping a table in MySQL will
automatically drop any indices on the table.  However, I'm not sure if
this is the case for all databases.  So would it be considered "good
style" to include the remove_index even though it's normally a bit
redundant?
72eb65fa089082d3bd8b38a5596ad331?d=identicon&s=25 Keynan Pratt (keynan)
on 2007-05-24 01:08
it is good style to remove foreign indexes as apposed to local indexes.
All modern DB's will drop the index pertaining to a column in this
table. The problem arises when an index in table "cats" is the foreign
key pointing to the id of table "dogs"

the index in "cats" must be removed to drop the "dogs" table.
This topic is locked and can not be replied to.