Search code examples
ruby-on-railsactiverecordrails-migrations

Can I add comments to a table or column using ActiveRecord Migrations?


In MySQL (and other SQL databases), it can be helpful to add comments to a table or column whose purpose may be unclear. (Search MySQL's create table syntax for "comment" for examples.)

Is there a way to do this in an ActiveRecord Migration? I have tried this with no results.

create_table :stuff do |t|
  t.integer :obscure_column, :comment => "Explanatory comment"
end

I'm using Rails 3.1.


Solution

  • The migration_comments gem mentioned in a comment to the original question appears to be the best cross-database solution for this need. In addition to providing migrations support for adding table and column comments, the gem also annotates the schema.rb file to include all of the comments. Perfect for my company's needs (large legacy rails app where the database model is ambiguous and also shared with a team of analysts writing native SQL reports).