Rails: How to add add_index to existing table

前端 未结 3 1942
礼貌的吻别
礼貌的吻别 2021-02-04 06:55

I already migrated a table called units with several columns. I was wondering how to migrate in a stand alone \'add_index\' to this table using the cmd. Is this code correct:

相关标签:
3条回答
  • 2021-02-04 07:15

    To remove an index, you must use remove_index with the same table and column specification as the self.up's add_index has. So:

    def self.down
      remove_index :units, :lesson_id
    end
    

    A multi-column index example would be:

    def self.down
      remove_index :units, [:lesson_id, :user_id]
    end
    
    0 讨论(0)
  • 2021-02-04 07:26

    Almost

    class AddIndexToUnits < ActiveRecord::Migration
      def self.up
        add_index :units, :lesson_id, :name=>'lesson_index'
      end
    
      def self.down
        remove_index :units, 'lesson_index'
      end
    end
    
    0 讨论(0)
  • 2021-02-04 07:32

    The self.up method is correct. Use this for your self.down:

    remove_index :units, :column => :lesson_id
    
    0 讨论(0)
提交回复
热议问题