# Migrations add\_index

**URL:** <https://www.ruby-forum.com/t/migrations-add-index/104924>\
**Category:** Rails\
**Created:** [May 23, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/migrations-add-index/104924 "2007-05-23T22:22:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 23, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/migrations-add-index/104924/1 "2007-05-23T22:22:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 24, 2007, 1:08am UTC](https://www.ruby-forum.com/t/migrations-add-index/104924/2 "2007-05-24T01:08:09Z")

</div>

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.
