What's the correct syntax for remove_index in a Rails 3.1.0 migration?

后端 未结 6 900
清歌不尽
清歌不尽 2021-02-01 00:30

I\'m in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out the following migration:

class         


        
6条回答
  •  走了就别回头了
    2021-02-01 01:01

    To alter a table and/or its indeces use #change_table inside #change action of a migration. Then you be able to create reversable index removal as follows:

    def change
       change_table :users do |t|
          t.index :email, :unique => true
          t.index :reset_password_token, :unique => true
       end
    end
    

    When you have to drop a table with its index of course with reversable action you can use #drop_table method for SchemaStatements with the #index method of Table class for ConnectionAdapter:

    def change
       drop_table :users do |t|
          t.index :email, :unique => true
          t.index :reset_password_token, :unique => true
       end
    end
    

    In case you have need exactly the #up/down pair in a migration. Use just a #change_table method along with #remove_index method of Table class for ConnectionAdapter:

    def up
       change_table :users do |t|
          t.index :email, :unique => true
          t.index :reset_password_token, :unique => true
       end
    end
    
    def down
       change_table :users do |t|
          t.remove_index :email, :unique => true
          t.remove_index :reset_password_token, :unique => true
       end
    end
    

    All of the methods are available in Rails version of 2.1.0 or of earlier ones.

提交回复
热议问题