0

I'm trying to add indexing to my users table for the email column. Typing rails g migration add_index_to_users_email generates the migration but the function is empty. The snake casing should be correct, so I'm at a loss as to why the migration is being created but the change function inside is empty.

I've also tried AddIndexToUsersName and the same issue arises.

Any direction on what the issue could be would be greatly appreciated. Only thing I can think of is that I'm using Postgres and not MySQL or SQLite, but that wouldn't matter would it?

2 Answers 2

1

As far as I know, migration generators only support addition and removal of columns, with a specified modifier. For example, if you wished to add a new string column phone to the users table, you could use the command

rails generate migration AddPhoneToUsers phone:string

Check the Rails Guides for column modifiers. You can try

rails generate migration AddIndexToUsers email:index

to add an index to the existing column. However, I am not sure if generators support column modification. You can write the migration yourself, assuming the email column already exists on the users table:

class AddIndexToUsers < ActiveRecord::Migration
  def change
    add_index :users, :email
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, that makes sense. I also see where I errored in assuming my original rails migration would work. I misread Hartl's explanation on what to do. Thanks.
No problem, glad it helped! Quick question, who is Hartl?
Hartl, I've really been digging the book. Very in depth giving thorough explanations to everything.
1

Have a look at:

http://guides.rubyonrails.org/active_record_migrations.html

The correct command is

rails g migration AddIndexToUsers email:string:index

This will generate:

class AddIndexToUsers < ActiveRecord::Migration
  def change
    add_column :users, :email, :string
    add_index :users, :email
  end
end

Edit the migration file and delete the add_column line, then run the migration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.