SQLite

・ファイルの場所

db/migrate/○○.rb

・migration のヘルプの見方
 1.Railsアプリのあるディレクトリに移動する。
 2.ruby script/generate migration --help

・migration ファイルの作り方(Railsのトップディレクトリで)
ruby script/generate migration create_[テーブル名]

これで、 db/migrate/001_create_[テーブル名].rb というファイルができる。
テーブルひとつに対してひとつ。

  • db/migrate/001_create_[テーブル名].rb - 修正前
class CreateBirthdayLists < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end
  • db/migrate/001_create_[テーブル名].rb - 修正後の例
class CreateBirthdayLists < ActiveRecord::Migration
  def self.up
    create_table(:birthday_lists) do |table|
      table.column(:company_directory_no, :integer, :null => false)
      table.column(:name, :string, :limit => '100', :null => false)
      table.column(:birthday, :date)
    end
  end

  def self.down
    drop_table(:birthday_lists)
  end
end

※idは自動的に作成される

・migration ファイルの説明
self.up : migration のバージョンがあがるときに実行される
sefl.down : migration のバージョンが下がるときに実行される

・rake db:migrate
migration ファイルを作成したら実行しましょう。テーブルが出来ます。


・参考
http://www.atmarkit.co.jp/im/carc/serial/proto04/proto04.html