Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Rails genrate migration add_index_to... does not actually add the index.

Rails Ruby migration

  • Please log in to reply
3 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 24 October 2013 - 05:55 PM

I created a users table via "rails generate model User name:string email:string ..."  the migration file was created as well.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

Now I want to add an index to the email column "following the tutorial"  I've done this successfully the first time through using sqlite3. Second time through im using MySql (mysql2).  Again created the table fine with generate model..  When I run the following:

rails generate migration add_index_to_users_email

the process ends with no error message and creates the migration file as shown below, but there is no setting of any index..

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
  end
end


Im expecting to see  add_index :users, :email, unique: true in there ...  Anybody have any idea's..  searched other threads to no avail..  running rails 4, mysql 5.6 ruby 1.9.3   my schema that was created after initil db:migrate is:

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

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.string   "mobile_phone"
    t.string   "mobile_phone_type"
    t.date     "birth_date"
    t.string   "user_type"
    t.string   "ss_num"
    t.boolean  "agree_to_terms"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end



#2 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 24 October 2013 - 07:04 PM

I solved my problem..   I had to use a slightly different version of the generate migration call, the following is what worked, perhaps its an mysql thing?  not sure..

 

rails generate migration AddIndexToUsers email:string:index



#3 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 31 October 2013 - 04:19 PM

Glad you got your issue sorted

 

Just for the sake of clarity

 

rails generate migration AddIndexToUsers email:string:index

 

is the same as

 

rails generate migration add_index_to_users email:string:index

 

The reason your original didn;t work is because the email part of the generate command was not a separate word.


Programming is just about problem solving!


#4 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 31 October 2013 - 05:17 PM

Glad you got your issue sorted

 

Just for the sake of clarity

 

rails generate migration AddIndexToUsers email:string:index

 

is the same as

 

rails generate migration add_index_to_users email:string:index

 

The reason your original didn;t work is because the email part of the generate command was not a separate word.

ah, yes...  thanks!!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users