Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 592
Nathan Long Avatar asked Nov 08 '11 17:11

Nathan Long


People also ask

What's the purpose of ActiveRecord?

Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

How do I add a reference to an existing table in rails?

When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.

What are active record migrations?

Migrations are a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use a Ruby DSL to describe changes to your tables.

What are ActiveRecord methods?

This method returns an array of column objects that Active Record considers the actual con- tent, or data, of the Active Record class. Therefore, this array does not include the primary ID column, any columns ending in _id or _count, and any columns used for single table inheritance.


1 Answers

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).

like image 137
QuarkleMotion Avatar answered Oct 14 '22 14:10

QuarkleMotion