index
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-3)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (-2)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
index(column_name, options = {})
public
Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.
t.index(:name) t.index([:branch_id, :party_id], unique: true) t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')
See {connection.add_index}[rdoc-ref:SchemaStatements#add_index] for details of the options you can use.
Re: Doesn't work? Don't think it ever has.
Instead of
create_table :user_follows, :force => true do |t| t.references :user t.references :followed_user t.timestamps t.index :user t.index :followed_user end
Try
create_table :user_follows, :force => true do |t| t.references :user t.references :followed_user t.timestamps end index :user_follows , :user index :user_follows , :followed_user
It looks like the provided examples are incorrect…
still broken
add_index is a different method. I think this is just a bug and it’s broken.
index doesn't work
@ssoroka even I’m having same issue. how did you resolve it?
@ssoroka and @drova and future readers
I guess these two have already found a solution, but future readers might have not. index and references do not map perfectly
change_table :foo do |t| t.references :bar t.index :bar_id end
references gets the model name while index gets the column name.
Not working in create_table
When using the index method within a create_table statement, it does not have any side effect - at least not in MySQL.
create_table :comment do |t| t.belongs_to :post t.timestamps # not working inside create_table ! t.index :post_id end
It is working properly in change_table though
change_table :comment do |t| t.belongs_to :user # this works inside change_table t.index :user_id end
Unfortunately this flaw is not reported in any way. The index is just not created.
I have only tested this with the mysql2 driver in Rails 2.3.x. I’m not sure, if this happens in other versions/adapters as well.
This method does not work.
It’s an old problem, reported back in 2010, just reopened issue:
Doesn't work? Don't think it ever has.
This doesn’t work for me. I do something like:
create_table :user_follows, :force => true do |t| t.references :user t.references :followed_user t.timestamps t.index :user t.index :followed_user end
and I get:
rake aborted! An error has occurred, all later migrations canceled: undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x106c02220>
add_index has the same effect.