LoginSignup
2
2

More than 3 years have passed since last update.

テーブルからカラムを削除する

Last updated at Posted at 2019-09-07

3行で

  • ターミナルにコマンドを書いてmigrationファイルを作る
  • migrationファイルの中身を編集する
  • rails db:migrate

くわしく

今回の場合、usersテーブルからageカラムを削除したい。

$ rails g migration RemoveAgeFromUsers

実行するとmigrationファイルが作られる。
こちらを編集する。

作成されたmigrationファイル
class RemoveAgeFromUsers < ActiveRecord::Migration[5.2]
  def change
    remove_column :Users, :age, :integer
  end
end

ポイントはデータ型を書くこと。
問題なければ下記のコマンドを入力する。

$ rails db:migrate
2
2
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
2
2