Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name an Index in a Migrate with index: true

I have a migrate, below, where I create an index with index: true. However, the name is too long for that index so I attempted to name it myself. However, that doesn't seem to run. I get the same "name too long" error. Is there a way to name an index like this with index: true? If not, how do I go about naming it with add_index?

class CreateVehicleProductApplicationNotes < ActiveRecord::Migration
  def change
    create_table :vehicle_product_application_notes do |t|
      t.references :product_id, index: true
      t.references :product_application_id, index: true, :name "my_index"
      t.references :note_id, index: true

      t.timestamps
    end
  end
end
like image 563
steventnorris Avatar asked Oct 16 '14 17:10

steventnorris


1 Answers

Instead of true, you can pass Hash containing name of the index as follows,

t.references :product_application_id, index: { name: "my_index" }

Reference: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html

like image 114
Nitish Parkar Avatar answered Oct 21 '22 16:10

Nitish Parkar