add_index(table_name, column_name, options = {}) public

Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

The index will be named after the table and the first column name, unless you pass :name as an option.

When creating an index on multiple columns, the first column is used as a name for the index. For example, when you specify an index on two columns [:first, :last], the DBMS creates an index for both columns as well as an index for the first column :first. Using just the first name for this index makes sense, because you will never have to create a singular index with this name.

Examples
Creating a simple index
 add_index(:suppliers, :name)

generates

 CREATE INDEX suppliers_name_index ON suppliers(name)
Creating a unique index
 add_index(:accounts, [:branch_id, :party_id], :unique => true)

generates

 CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
Creating a named index
 add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')

generates

 CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
Show source
Register or log in to add new notes.
July 10, 2008
1 thank

migration example

def self.up create_table :regs do |t|

    t.column :login, :string, :limit=>'10'
    t.column :pass, :string, :limit=>'10'
    t.column :email, :string, :limit=>'20'
    t.column :fio, :string, :limit=>'30'
    t.column :born, :date
    t.column :phone_code, :integer, :limit=>'3'
    t.column :phone_post, :integer, :limit=>'7'
    t.column :password, :string, :limit=>'20'
    t.column :pass_when, :date
    t.column :pass_who, :string,:limit=>'30'
    t.column :wmid, :integer, :limit=>12
    t.column :wmr, :integer, :limit=>12
    t.column :wmz, :integer, :limit=>12
    end

add_index :regs, [:login, :wmr, :wmz], :unique => true end

July 10, 2008
This note might be spam - Undo spam marking - Show