Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify custom index name when using add_reference

I have the following migration

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
  def up
    add_reference :doctors, :doctor_specialization, polymorphic: true, index: true
  end

  def down
    remove_reference :doctors, :doctor_specialization, polymorphic: true
  end
end

when i run rake db:migrate i am getting the error

Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters

so how can i specify the index name when using add_reference like the way we specify in add_index :table, :column, :name => 'index name'

like image 570
bhanu Avatar asked May 21 '15 06:05

bhanu


2 Answers

As I commented, do :

add_index :table, :column, name: 'index name' 

Here is documentation. Or, you can try this :

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
  def change
    add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index name' }
  end
end
like image 144
Arup Rakshit Avatar answered Nov 01 '22 21:11

Arup Rakshit


This would actually work:

add_index :table, :column, name: 'index name'

Take a look for more examples.

like image 4
Andrey Deineko Avatar answered Nov 01 '22 21:11

Andrey Deineko