LoginSignup
1
1

More than 5 years have passed since last update.

【Rails】マイグレーションでカラムの型を変更する

Posted at

環境

  • Ruby: 2.6.3
  • Rails: 5.2.3
  • PostgreSQL: 9.6

書き方

class ChangeColumnsToUser < ActiveRecord::Migration[5.2]
  def up
    change_column :users, :birthday, :date, using: "birthday::date", comment: "生年月日"
    change_column :users, :sex, :integer, using: "sex::integer", comment: "性別"
  end

  def down
    change_column :users, :birthday, :text, comment: "生年月日"
    change_column :users, :sex, :text, comment: "性別"
  end
end

補足

usingオプションを付けないとこのようなメッセージが表示され、マイグレーションが失敗します。

HINT:  You might need to specify "USING birthday::date".
1
1
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
1
1