Specify custom index name when using add_reference

瘦欲@ 提交于 2020-07-04 20:53:29

问题


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'


回答1:


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



回答2:


This would actually work:

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

Take a look for more examples.




回答3:


I would not suggest leaving out "add_reference", but you could leave out the "index" option hash key and then use "add_index":

add_reference :table, :column
add_index :table, :column, :name => 'index_table_column'

Or, the more appropriate way would be like this:

add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index_doctors_doctor_specialization' }



回答4:


I've heard the best way to fix this is to just leave it out of the add reference line and specify it manually below much like in the last line of your question

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


来源:https://stackoverflow.com/questions/30366315/specify-custom-index-name-when-using-add-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!