Cannot delete index from table in Rails 4 and PSQL 9.3

In my schema.rb, I have the following line:

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 

When I run \di in psql, I get:

 Schema | Name | Type | Owner | Table --------+--------------------------------------------------------------+-------+-------+----------------------- public | index_users_on_email | index | alex | users 

However, if you include one of them in the migration:

  • remove_index: users, name :: index_users_on_email
  • remove_index: users, column :: email
  • remove_index: users: email
  • execute 'DROP INDEX index_users_on_email'

I get the following error:

 rake aborted! An error has occurred, this and all later migrations canceled: Index name 'index_users_on_email' on table 'users' does not exist 

I also found this issue . So any ideas?

+6
source share
3 answers

If you want to remove the index by name, you can write it as follows:

 remove_index(:table_name, :name => 'index_name') 

You should also look at this question: What is the correct syntax for remove_index in Rails 3.1.0 migration?

+7
source

I know this happens very late, but I just ran into something similar. Is it likely that you renamed the "email" column before trying to delete the index? If so, renaming the column will remove the index. Thus, the order should be:

1) delete index 2) rename column

+5
source

A bit late answer, but I just came across the same question, so I write this answer in the hope that it will help others in the future.

The issue encountered by the initial request led to a solution. It seems that in Rails 3.x the implementation of index_name_for_remove was erroneous, so sometimes it did not accept named indexes. Instead, he raised an ArgumentError , claiming that the index does not exist.

In Rails 4.x, the implementation was changed so that the hash transfer with the index name was handled correctly (I think I'm doing no Rails 4 application for testing at the moment).

If you need to handle this in Rails 3.x, looking at the remove index source will lead to a solution - using the remove_index! method remove_index! . remove_index! takes two arguments - the name of the table and the name of the index. So, in the case of the original post question, this command should complete the task:

remove_index! :users, :index_users_on_email

Refresh . After posting the answer, I noticed that the original poster asked a question about Rails 4, so it would seem that in some cases there are still problems with the implementation of index_name_for_remove that does not detect the correct index name. However, since the index name is known and we don’t really need to know what the name is (which is what index_name_for_remove does internally), we can still use the remove_index! method remove_index! to remove it.

+1
source

Source: https://habr.com/ru/post/956489/


All Articles