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
How to remove index in 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"

