steventnorris
steventnorris

Reputation: 5896

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

Upvotes: 7

Views: 1220

Answers (1)

Nitish Parkar
Nitish Parkar

Reputation: 2868

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

Upvotes: 9

Related Questions