Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove index in rails

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.

 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!

like image 420
Tom Hammond Avatar asked Mar 30 '14 15:03

Tom Hammond


People also ask

Does removing column delete index?

16, removing the column removes the index.

How do you delete a column in Rails?

The change method can be used to drop a column in Rails 4 applications, but should not be used in Rails 3. I updated my answer accordingly. You can also use remove_column :table_name, :column_name, :type, :options within the change method, since if you specify the type reverting the migration is possible.

What is index in Ruby on Rails?

An index is used to speed up the performance of queries on a database. Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending. But consider the case where we are fetching reports from the database.


1 Answers

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" 
like image 119
vee Avatar answered Sep 19 '22 14:09

vee