23
24

More than 5 years have passed since last update.

[Rails] PostgreSQLのテーブルやカラムにコメントをつけてmigrationする

Posted at

調べるといろいろそれっぽい情報はでてきましたが、古い情報が多く、いろいろ試した結果 Rails 4.1 でまともに使えたのは migration_comments だけでした。

下記のようにオプションとして :comment が渡せるようになったり、

db/migrate/20140627012348_add_prefecture_id_to_users.rb
class AddPrefectureIdToUsers < ActiveRecord::Migration
  def change
    add_column :users, :prefecture_id, :integer, comment: '都道府県ID(prefectures.id)'
  end
end

下記のようにコメントを付与するだけの migration を作ったりもできます。

db/migrate/20140515025651_add_comments_to_users.rb
class AddCommentsToUsers < ActiveRecord::Migration
  def up
    change_table :users do |t|
      t.comment 'ユーザを管理するテーブル'
      t.change_comment :name, '名前'
      t.change_comment :email, '電子メールアドレス'
    end
  end
end

うまく動かなかった gem

ちなみに試した gem は下記があります。

  • column_comment
  • schema_comments
  • pg_power
23
24
1

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
23
24