Ruby-on-rails 如何删除导轨中的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22745757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:29:20  来源:igfitidea点击:

How to remove index in rails

ruby-on-rails

提问by Tom Hammond

I have found that I have two "survey_id" columns in my schema and that's been causing some problems for me. Specifically I need to remove the second index as I don't want survey_id to be unique.

我发现我的架构中有两个“survey_id”列,这给我带来了一些问题。具体来说,我需要删除第二个索引,因为我不希望 Survey_id 是唯一的。

 add_index "completions", ["survey_id"], name: "index_completions_on_survey_id"
 add_index "completions", ["survey_id"], name: "index_completions_on_survey_id_and_user_id", unique: true

I've tried

我试过了

def change
   remove_index "completions", ["survey_id"], name => "index_completions_on_survey_id_and_user_id"
 end

and

def change
   remove_index "completions", ["survey_id"], name: "index_completions_on_survey_id_and_user_id"
 end

But neither of those seem to work. What's the correct syntax for this migration to remove the index? I feel like this is basic and I'm just missing something stupid. Thanks in advance!

但这些似乎都不起作用。此迁移删除索引的正确语法是什么?我觉得这是基本的,我只是错过了一些愚蠢的东西。提前致谢!

回答by vee

You don't supply the columns in the index when removing one. Try:

删除列时,您不提供索引中的列。尝试:

remove_index :completions, name: "index_completions_on_survey_id_and_user_id"

回答by Dheepak

From rails console, run following

从 rails 控制台,运行以下

ActiveRecord::Migration.remove_index "completions", name: "index_completions_on_survey_id_and_user_id"