Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails generate migration add_index_to... does not put the actual index in the migration file

Tags:

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 
like image 770
JaySchrock Avatar asked Oct 24 '13 17:10

JaySchrock


People also ask

What does Rails generate migration do?

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.

What is index in Rails migration?

An index is used to speed up the performance of queries on a database. Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending. But consider the case where we are fetching reports from the database.

Where does Rails store migration data?

Rails stores the most recent database schema in the file db/schema. rb . This file is the Ruby representation of all the migrations run on your database over the life of the application.

How do I migrate a specific 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.


1 Answers

via http://guides.rubyonrails.org/migrations.html

If you'd like to add an index on the new column, you can do that as well:

$ rails generate migration AddPartNumberToProducts part_number:string:index

your generator

rails generate migration add_index_to_users_email 

simply creates an empty migration file and did not describe a index

so this would be more appropriate...

rails generate migration AddIndexToUsers email:string:index 

should give you

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

Nguyen You - EDIT

This command [Rails 5.2.3]

rails generate migration AddIndexToUsers email:string:index 

actually will give you

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

not only add_index but also add_column to the users table.

like image 162
blotto Avatar answered Sep 16 '22 15:09

blotto