Rails:如何将add_index添加到现有表

yos*_*osh 15 indexing ruby-on-rails migrate

我已经迁移了一个名为units的表,其中有几列.我想知道如何使用cmd将独立的"add_index"迁移到此表中.这段代码是否正确:

class AddIndexToUnits < ActiveRecord::Migration
  def self.up
    add_index :units, :lesson_id
  end

  def self.down
    remove :units
  end
end
Run Code Online (Sandbox Code Playgroud)

我有一种自我感觉可能是错误的,我不确定.

Kri*_*ris 14

self.up方法是正确的.用于你的self.down:

remove_index :units, :column => :lesson_id
Run Code Online (Sandbox Code Playgroud)


Yul*_*ule 10

几乎

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
Run Code Online (Sandbox Code Playgroud)