instance method add_index

Ruby on Rails 3.0.20

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

add_index(table_name, column_name, options = {})

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)
Creating an index with specific key length
add_index(:accounts, :name, :name => 'by_name', :length => 10)

generates

CREATE INDEX by_name ON accounts(name(10))

add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})

generates

CREATE INDEX by_name_surname ON accounts(name(10), surname(15))

Note: SQLite doesn’t support index length

Parameters

table_name req
column_name req
options opt = {}
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 329
      def add_index(table_name, column_name, options = {})
        column_names = Array.wrap(column_name)
        index_name   = index_name(table_name, :column => column_names)

        if Hash === options # legacy support, since this param was a string
          index_type = options[:unique] ? "UNIQUE" : ""
          index_name = options[:name].to_s if options.key?(:name)
        else
          index_type = options
        end

        if index_name.length > index_name_length
          raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
        end
        if index_name_exists?(table_name, index_name, false)
          raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
        end
        quoted_column_names = quoted_columns_for_index(column_names, options).join(", ")

        execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})"
      end

Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 329 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::SchemaStatements

Type at least 2 characters to search.

↑↓ navigate · open · esc close