What is the correct syntax for remove_index in Rails 3.1.0 migration?

I am trying to add Devise to an existing Rails application with an already defined Users table. The project generator supplanted the following migration:

class AddDeviseToUsers < ActiveRecord::Migration def self.up change_table(:users) do |t| ## Database authenticatable t.string :email, :null => false, :default => "" t.string :encrypted_password, :null => false, :default => "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable t.integer :sign_in_count, :default => 0 blah blah blah.... end add_index :users, :email, :unique => true add_index :users, :reset_password_token, :unique => true end 

Migration down is not generated, and I have time to delete these indexes. I see various suggestions in the documentation and various suggestions on the Internet, but none of them seem to work for me. For example...

 def self.down change_table(:users) do |t| t.remove :email t.remove :encrypted_password t.remove :reset_password_token blah blah blah... end remove_index :users, :email remove_index :users, :reset_password_token end 

leads to...

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

which is odd because if I check the database, I'm pretty sure 'index_users_on_email' is right there ...

I tried other options, including

 remove_index :users, :column => :email remove_index :users, 'email' 

or

 change_table(:users) do |t| t.remove_index :email end 

... but without dice. I am running Rails 3.1.0, Ruby 1.9.2, rake 0.9.2.2, with Postgres.

The command that lets me go:

 bundle exec rake db:rollback STEP=1 

after successful migration. Any tips?

+56
ruby-on-rails rails-migrations
Jan 27 2018-12-12T00:
source share
6 answers

Depending on the type of database, you do not need to worry about deleting indexes in the self.down method, since the index will be automatically deleted from the database when the column is deleted.

You can also use this syntax in your self.down method:

 def self.down remove_column :users, :email remove_column :users, :encrypted_password remove_column :users, :reset_password_token end 
+40
Jan 27 '12 at 3:40
source share

For writing, the method of deleting an index by name

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

therefore in your case

 remove_index(:users, :name => 'index_users_on_email') 
+146
Jul 12 2018-12-12T00:
source share

You can also remove the index defining columns that are less error prone in my opinion than writing a name

 remove_index :actions, :column => [:user_id, :action_name] 
+52
Oct 02 '12 at 10:52
source share

I would like to extend the answer to @iWasRobbed. If you have an index for only one column, then worrying about remove_index does not make sense since (just a guess!) The database should be smart enough to clean up the resources used by this index. But if you have multiple columns, the index removing the column will reduce the index to the columns that still exist, which is quite reasonable, but will show where you can use remove_index .

Just to illustrate, the migration below has the disadvantage that after applying up and down, it leaves a unique index on email (this means that the down part is not doing its job properly)

 class AddIndexes < ActiveRecord::Migration def up add_column :users, :action_name, :string add_index :users, [:email, :action_name], unique: true end def down remove_column :users, :action_name end end 

Change down block to

  def down remove_index :users, [:email, :action_name] remove_column :users, :action_name end 

will fix this drawback and allow the migration to correctly return the DB to its previous state using rake db:rollback

+5
Nov 15 '13 at 12:58
source share

To modify a table and / or its indexes, use #change_table inside the #change migration action. You can then create a reversible index deletion as follows:

 def change change_table :users do |t| t.index :email, :unique => true t.index :reset_password_token, :unique => true end end 

When you need to drop a table with a course index using a reversible action, you can use the #drop_table method for SchemaStatements using the #index Table method for ConnectionAdapter :

 def change drop_table :users do |t| t.index :email, :unique => true t.index :reset_password_token, :unique => true end end 

If you need just the #up/down pair in the carry. Use only the #change_table along with the #remove_index method of the Table class for ConnectionAdapter :

 def up change_table :users do |t| t.index :email, :unique => true t.index :reset_password_token, :unique => true end end def down change_table :users do |t| t.remove_index :email, :unique => true t.remove_index :reset_password_token, :unique => true end end 

All methods are available in Rails version 2.1.0 or earlier.

0
November 26 '15 at 7:26
source share

Here is my full run of this (in Rails 5):

I have team_id as an index in table producers. I no longer need this attitude. To get rid of him. Did the following:

1) create a migration.

  $ rails generate migration RemoveTeam_idFromVendor team_id:integer 

2) After starting the migration, give me this error. And this is because there are rows in the provider table whose foreign key refers to the primary key value of the command table.

 == 20170727202815 RemoveTeamIdFromVendor: migrating =========================== -- remove_column(:vendors, :team_id, :integer) rake aborted! StandardError: An error has occurred, this and all later migrations canceled: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "vendors" 

3) To solve this problem and perform the migration, I did the following (Note: I'm in dev):

 $ rake db:drop Dropped database 'db/development.sqlite3' Dropped database 'db/test.sqlite3' $ rake db:create Created database 'db/development.sqlite3' Created database 'db/test.sqlite3' $ rake db:migrate ~ ~ ~ == 20170727202815 RemoveTeamIdFromVendor: migrating =========================== -- remove_column(:vendors, :team_id, :integer) -> 0.0185s == 20170727202815 RemoveTeamIdFromVendor: migrated (0.0185s) ================== 
0
Jul 28 '17 at 15:08
source share



All Articles