Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add syntactic sugar for indexes in migrations

#1656

The current "sexy" syntax for creating and changing tables in migrations look very different from the way indexes are added and removed:

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :title, :null => false
      t.text :body
      t.date :published_at
      t.references :author, :null => false
    end
    add_index :articles, :published_at  # not very DRY
  end
end

A more concise syntax can be achieved:

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :title, :null => false
      t.text :body
      t.date :published_at
      t.references :author, :null => false
      t.index :published_at # <-- pretty dry!
    end
  end
end

A complementary option would be t.date :published_at, :index => true. This, however, ties the definition of the column with the addition of the index, which wouldn't be nice in change_table.

Reported by Daniel Schierbeck · December 28th, 2008 @ 06:16 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Daniel Schierbeck
    Daniel Schierbeck
    • Tag cleared.

    Never mind, this is actually already possible! I just could immediately find where it was defined -- it's in ActiveRecord::ConnectionAdapters::Table... Sorry!

    December 28th, 2008 @ 06:21 PM

  2. Frederick Cheung
    Frederick Cheung
    • State changed from new to invalid

    December 28th, 2008 @ 06:36 PM