Rails:添加列后添加索引 [英] Rails: Adding an index after adding column

查看:654
本文介绍了Rails:添加列后添加索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Rails应用程序中创建了一个表。一段时间后,我添加一个列运行:

Suppose I created a table table in a Rails app. Some time later, I add a column running:

rails generate migration AddUser_idColumnToTable user_id:string. 

然后我意识到我需要添加 user_id 作为一个指标。我知道 add_index 方法,但是这个方法应该在哪里调用?我应该运行迁移(如果是,哪一个?),然后手动添加此方法?

Then I realize I need to add user_id as an index. I know about the add_index method, but where should this method be called? Am I supposed to run a migration (if yes, which one ?), then adding by hand this method?

推荐答案

你可以仅针对索引运行另一次迁移:

You can run another migration, just for the index:

class AddIndexToTable < ActiveRecord::Migration
  def change
    add_index :table, :user_id
  end
end

这篇关于Rails:添加列后添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆