简体   繁体   中英

schema.rb doesnt include add_index method after migration using postgresql

So I was trying to create a database index on the email column of a Users model, but I must be doing something wrong, since after I do the migration I go and check on the schema.rb file to see if the add_index method is included and nothing shows up. I am using postgresql , by the way. So here is what I did... I created the migration

rails generate migration add_index_to_users_email

After that, I edited and saved the db/migrate/20140911192804_add_index_to_users_email.rb file with the following code for indexing:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

After that I ran on the console

bundle exec rake db:migrate

And when I went to check on the schema.rb file to see if the add_index method was included, I found that it wasnt there. Here is what my schema.rb looked like

ActiveRecord::Schema.define(version: 20140911192804) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

I tried to run rake db:rollback to run again db:migrate and see if some "magic" occurred but I wasnt even able to rollback, getting this error message:

   ==  AddIndexToUsersEmail: reverting ===========================================
-- remove_index(:users, {:unique=>true, :column=>:email})
  ←[1m←[35m (0.0ms)←[0m  ROLLBACK
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'index_users_on_email' on table 'users' does not existC:in `migrate'
Tasks: TOP => db:rollback

I'm pretty lost... something that i found interesting was that in the schema.rb file this line

ActiveRecord::Schema.define(version: 20140911192804) do

had the same timestamp as the migration file for the add_index db/migrate/20140911192804_add_index_to_users_email.rb

So there was some sort of update on the schema.rb file during the migration but not what I was expecting to occur.

I don't even know where to start :D so I hope someone a bit more experienced can give me a hint.

Thanks so much!

After hours of trial and error I could finally find a solution!

For some reason beyond my understanding, the migration didnt add the add_index method to my schema.rb, however, when I tried to rollback, it was looking for an index in the users table that didnt exist, so it kept aborting the rollback.

I assumed that the info about the index that it had to look for in the table was in the migration file. So I deleted the content of the migration file leaving it like this:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
  end
end

I was finally able to rollback.

Then I typed again the content inside the AddIndexToUsersEmail migration file

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

I ran again bundle exec rake db:migrate and it worked! :D

Thanks a lot to everyone who took their time to read this issue!

I just had the same problem and found why !!!

You (we) used the migrate command before saving the migrate file !! So the migration was passed with nothing inside. Just a change method with no instructions.

And if after you try to rollback and the migration file have been saved since. The add_index instruction is in so it can't rollback.

I hope i am clear enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM