Specify custom index name when using add_reference

前端 未结 4 1243
生来不讨喜
生来不讨喜 2021-02-12 15:31

I have the following migration

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
  def up
    add_reference :doctors, :doctor_specialization, poly         


        
相关标签:
4条回答
  • 2021-02-12 15:54

    This would actually work:

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

    Take a look for more examples.

    0 讨论(0)
  • 2021-02-12 15:56

    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' }
    
    0 讨论(0)
  • 2021-02-12 16:04

    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
    
    0 讨论(0)
  • 2021-02-12 16:09

    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'
    
    0 讨论(0)
提交回复
热议问题