LoginSignup
244
227

More than 5 years have passed since last update.

マイグレーションを使ったカラムの追加、削除、データ型の変更 [ 自分用メモ ]

Last updated at Posted at 2016-03-08

カラムの追加

やり方1

$ rails g migration Addカラム名Toテーブル名 カラム名:型名
例:$ rails g migration AddHogeToHuga hoge:string ※ 外部キーのカラムのデータ型はinteger

※最初のカラム名:先頭大文字
※テーブル名:先頭大文字
※最後のカラム名:先頭小文字
※型名:全て小文字
※テーブル名は元からある名前でOK。例) inquiriesとか。
「Addカラム名Toテーブル名」のカラム名とテーブル名は先頭を大文字にして記入します。
これでマイグレーションファイルを生成することができる。

※上記コマンドを実行したら、作成されたファイルを確認する
2つ目、3つ目のカラムを追加する場合は、作成されたマイグレーションファイルを編集し、保存する。

最後に
開発環境にマイグレーション処理を実行する
$ rake db:migrate
を実行してデータベースに反映させます。(生成されたマイグレーションファイルは削除しなくてもOK
※ コンソールでエラーが出たら、このコマンドを実行してない可能性が高い

本番環境にマイグレーション処理を実行する
$ git push heroku master 後に
$ heroku run bundle exec rake RAILS_ENV=production db:migrate

開発環境
rails dbconsoleにして、
\d inquiries; を実行し、テーブルにカラムが追加されたか確認する。

本番環境
heroku pg:psql
SELECT * FROM テーブル名;
以上

やり方2 ※外部キーを追加するときのみ

referencesを使用する
http://qiita.com/sinagaki58/items/7edea51ef00e393834ca

今回はtweetsモデルにuser_idを追加します。

user.rb
has_many :tweets, dependent: :destroy

rails g migration AddUserRefToTweets user:references

マイグレーション.rb
class AddUserRefToTweets < ActiveRecord::Migration
  def change
    add_reference :tweets, :user, index: true
  end
end

※referencesやbelongs_toを使うときは、カラムネームを指定する際に_id接尾子をつけません。


カラムの削除

rails g migration remove_カラム名 _ to _ テーブル名 カラム名
例) rails g migration remove_hoge_to_fuga hoge
これでマイグレーションファイルを生成することができる。
        ↓
生成されたファイルを確認する
例)
remove_column :companies, :company_name, :string
        ↓
あとは
rake db:migrate
を実行してデータベースに反映させる。

これで開発環境のテーブルからはカラムを削除できる。
但し、本番環境のテーブルからも削除するには、

heroku run bundle exec rake RAILS_ENV=production db:migrate
を、実行する。

データ型の変更(change_column)

rails g migration change_datatype_title_of_articles
rails g migration change_datatype_カラム名 _ of _ テーブル名

db/migrate/20140808183810_change_datatype_title_of_articles.rb
def change
    # [形式] change_column(テーブル名, カラム名, データタイプ, オプション)
    change_column :articles, :title, :text
end

カラム名の変更

テーブル名:job_administrations
変更したいカラム名:request
変更後のカラム名:status

カラム名を変更するためのmigrationファイルを作成します。

$ rails g migration rename_request_column_to_job_administrations

db/migrate/20150629074110_rename_request_column_to_job_administrations.rb っていうファイルが作成されます。

作成されたファイルを開くと

マイグレーション.rb
class RenameRequestColumnToJobAdministrations < ActiveRecord::Migration[5.0]
  def change
    rename_column :job_administrations, :request, :status
  end
end

後からデフォルト値の追加、NULL制約の追加を行う

rails g migration クラス名(任意 + クラス名)
rails g migration addDefaultToSailor_job_informations

db/migrate/20170504074521_add_default_to_sailor_job_informations.rb
class AddDefaultToSailorJobInformations < ActiveRecord::Migration[5.0]
  def change
    # デフォルト値を追加
    change_column_default :sailor_job_informations, :request, false
    change_column_default :sailor_job_informations, :inexperienced_person_search, false
    change_column_default :sailor_job_informations, :educational_qualification, false
    change_column_default :sailor_job_informations, :help_wanted, false
    change_column_default :sailor_job_informations, :new_arrival_job, false
    change_column_default :sailor_job_informations, :second_graduate, false

    # NULL制約を追加
    change_column_null :sailor_job_informations, :request, false
    change_column_null :sailor_job_informations, :inexperienced_person_search, false
    change_column_null :sailor_job_informations, :educational_qualification, false
    change_column_null :sailor_job_informations, :help_wanted, false
    change_column_null :sailor_job_informations, :new_arrival_job, false
    change_column_null :sailor_job_informations, :second_graduate, false
  end
end
244
227
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
244
227