LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby On Rails】 既存カラムにコメントを追加したい。

Last updated at Posted at 2022-09-30

Railsで、既存のカラムにコメントをしたかった。
忘れそうなので、やり方をメモします。
まず、マイグレーションファイルを作成し、以下のように実装します

class AddColumnComments < ActiveRecord::Migration[6.0]
  def change
    change_column_comment(:table, :column, 'コメント')
  end

基本的にはこれで大丈夫です!
こんな感じで、カッコなしでも指定できます。

change_column_comment :table, :column, 'コメント'

ちなみに、上記の実装だと、マイグレーションをdownやロールバックした際こんなエラーが発生します。

ActiveRecord::IrreversibleMigration: 

change_column_comment is only reversible if given a :from and :to option.

なので、こんな感じで

 change_column_comment(:table, :column, from: "旧コメント", to: "新コメント")

from, toオプションをつけておくと安心かもしれませんね!

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