Migration Details

To change schema, we use migration file(system).

How to make migration file

  1. rails generate model with Model
  2. rails generate migration with Migration file

How to make migration file by name

We can use following

** Recommendation **
Add operation name to migration file name
AddXxxxToTableName, RemoveXxxxFromTableName etc…

rails generate migration AddPriceToItems price:integer

How to implement migration file

class CreateItems < ActiveRecord::Migration def change create_table :items do |t| t.integer :user_id t.string :title t.string :memo t.datetime :created t.timestamps end end end [/bash] We add type definition into change method.
Use class which extends ActiveRecord::Migration
For other objectives, we use up, change, down methods.
Separate operations or get all together in change.


class AddTitleToItems < ActiveRecord::Migration
   def up
      add_column :items, :title, :string
   def down
      remove_column :items, :title

<h4>change method</h4>
class AddTitleToItems < ActiveRecord::Migration
   def change
      add_column :items, :title, :string

<h3>Create table options</h3>
create_table :items[,toptions] do |t|
   t.type fname

You can add table options

Option Description Default Value
:id Whether id is generated as primary key true
:primary_key Primary key name(if :id is true) id
:temprary Create temporary table name false
:force When creating table, drop old table or not false
:options Others

What is :option?
Example) Mysql InnoDB

:options => 'ENGINE=InnoDB CHARSET=utf-8'

Options Example

create_table(:objects, primary_key: 'guid') do |t|
  t.column :name, :string, limit: 80

Field Definition

Example: t.datatype :columnname
Field definition should be under create_table.

Migration SQLite Ruby
integer INTEGER Fixnum
decimal DECIMAL BigDecimal
float FLOAT Float
string VARCHAR(255) String
text TEXT String
binary BLOB String
date DATE Date
datetime DATETIME Time
timestamp DATETIME Time
time TIME Time
boolean BOOLEAN TrueClass/FalseClass

Migration type is type you use in create_table method.
SQLite is SQLite prepared data type, Ruby is ruby class

Wow, Blob is String in Ruby?

Column Flag

t.datatype :columnname, :flagname => value

Flag Description
:limit Limitation number
:default Default value
:null null is allowed?
:precision Total digit
:scale digit under a decimal point


t.string :name, :limit => 17, :null => false
t.string :title :limit => 100, :null => false
t.decimal :price, :precision => 5, :scale => 0
t.string publish, :limit => 20, :default => 'ABC出版'

Migration Methods

You can use following methods

Name Description
add_column Add new column
add_index Add new index
add_timestamps Add created_at, updated_at
change_column Change definition of column
change_column_default Change default value
change_table Change table definition
column_exists? Check column is existed or not
create_table Create new table
drop_table Delete table
index_exists? Check index is existed or not
remove_column Delete column
remove_index Delete index
remove_timestamps Delete existed created_at, update_at, there are added by default
rename_column Change column name
rename_index Change index name
rename_table Change table name
execute Execute SQL directly

Change table definition

Use change_table
Table layout, index add/delete etc…


change_table :books do |t~
   t.string :author # Add author column(string)
   t.remove :publish, :cd   # Delete publish, cd column
   t.index :title   # Add index to title
   t.rename :isbn, :isbn_code   # Change column name from isbn to isbn_code


There are methods we can use in change_table

  • index
  • change
  • change_default
  • rename
  • remove
  • remove_references
  • remove_index
  • remove_timestamps

Index(add_index, remove_index)


Option Description
:unique Add unique constraint or not
:name Index name
:length Length included index(not for SQLite)


add_index :books, title
add_index :books, [:publish, :title]    # multi column index
add_index :books, [:publish, :title], :unique => true, :name => 'idx_pub_title'
remove_index :books
remove_index :[:publish, :title]


Execute SQL directly.


  • Add special data type
  • Foreign key constraint
  • Check constraint
  • Other database special setting


execute "CREATE VIEW view_books AS SELECT * FROM books WHERE title = 'ABC'"

Be careful to execute SQL because sometimes doesn’t work after changeing!