0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Railsで既存のテーブルのカラムを追加・編集・削除する方法

Posted at

今回は「users」というモデルのカラムを色々いじくりたいとします

マイグレーションファイルを作成

rails generate EditColumn

dbディレクトリ配下にできたマイグレーションファイルをいじっていきます

###「name」カラムを追加するとき

20200301090906_edit_column.rb
class EditColumn < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :name, :string
  end
end

###既存の「age」カラムを「nenrei」に変更するとき

20200301090906_edit_column.rb
class EditColumn < ActiveRecord::Migration[5.2]
  def change
    rename_column :users, :age, :nenrei
  end
end

###既存の「age」カラムを削除

20200301090906_edit_column.rb
class EditColumn < ActiveRecord::Migration[5.2]
  def change
    remove_column :users, :age, :string
  end
end
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?