LoginSignup
0
0

More than 3 years have passed since last update.

[Rails] マイグレーションファイルの新規作成、書き方

Posted at

マイグレーションファイルの新規作成コマンド
rails g migration <migration-file-name>

例えば、usersテーブルにカラムを追加、削除するマイグレーションファイルを作成するなら下記のように書く。
rails g migration add_remove_column_to_users

マイグレーションファイルの中には下記のように書く。

def change
  # usesテーブルにbirthdayカラムを追加
  add_column :users, :birthday, :string
  # usesテーブルにageカラムを削除
  remove_column :users, :age, :string

end

最後に、
rails g db:migrate
をすればマイグレーションファイルがDBに反映されます。

0
0
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
0
0