Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct syntax for remove_index in a Rails 3.1.0 migration?

I'm in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out 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 

The downward migration isn't generated, and I'm having a heck of a time removing those indexes. I'm seeing different suggested notation in the documentation, and different suggestions online, but none of them seem to be working 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 

results in...

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, sure enough, 'index_users_on_email' is right there...

I've tried other variations, including

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

or:

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

...but no dice. I'm running Rails 3.1.0, Ruby 1.9.2, rake 0.9.2.2, with Postgres.

The command that's letting me down is:

bundle exec rake db:rollback STEP=1 

after successfully apply the migration up. Any advice?

like image 551
doublea Avatar asked Jan 27 '12 02:01

doublea


People also ask

How do you run down migration in rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

What is a migration in rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


1 Answers

For the record, the way to remove an index by name is

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

so in your case

remove_index(:users, :name => 'index_users_on_email') 
like image 161
Dty Avatar answered Oct 13 '22 13:10

Dty