add_column
add_column(table_name, column_name, type, options = {})
public
Add a new type column named column_name to table_name.
The type parameter is normally one of the migrations native types, which is one of the following: :primary_key, :string, :text, :integer, :bigint, :float, :decimal, :numeric, :datetime, :time, :date, :binary, :boolean.
You may use a type not in this list as long as it is supported by your database (for example, “polygon” in MySQL), but this will not be database agnostic and should usually be avoided.
Available options are (none of these exists by default):
-
:limit - Requests a maximum column length. This is number of characters for a :string column and number of bytes for :text, :binary and :integer columns.
-
:default - The column’s default value. Use nil for NULL.
-
:null - Allows or disallows NULL values in the column. This option could have been named :null_allowed.
-
:precision - Specifies the precision for the :decimal and :numeric columns.
-
:scale - Specifies the scale for the :decimal and :numeric columns.
Note: The precision is the total number of significant digits and the scale is the number of digits that can be stored following the decimal point. For example, the number 123.45 has a precision of 5 and a scale of 2. A decimal with a precision of 5 and a scale of 2 can range from -999.99 to 999.99.
Please be aware of different RDBMS implementations behavior with :decimal columns:
-
The SQL standard says the default scale should be 0, :scale <= :precision, and makes no comments about the requirements of :precision.
-
MySQL: :precision [1..63], :scale [0..30]. Default is (10,0).
-
PostgreSQL: :precision [1..infinity], :scale [0..infinity]. No default.
-
SQLite3: No restrictions on :precision and :scale, but the maximum supported :precision is 16. No default.
-
Oracle: :precision [1..38], :scale [-84..127]. Default is (38,0).
-
DB2: :precision [1..63], :scale [0..62]. Default unknown.
-
SqlServer?: :precision [1..38], :scale [0..38]. Default (38,0).
Examples
add_column(:users, :picture, :binary, limit: 2.megabytes) # ALTER TABLE "users" ADD "picture" blob(2097152) add_column(:articles, :status, :string, limit: 20, default: 'draft', null: false) # ALTER TABLE "articles" ADD "status" varchar(20) DEFAULT 'draft' NOT NULL add_column(:answers, :bill_gates_money, :decimal, precision: 15, scale: 2) # ALTER TABLE "answers" ADD "bill_gates_money" decimal(15,2) add_column(:measurements, :sensor_reading, :decimal, precision: 30, scale: 20) # ALTER TABLE "measurements" ADD "sensor_reading" decimal(30,20) # While :scale defaults to zero on most databases, it # probably wouldn't hurt to include it. add_column(:measurements, :huge_integer, :decimal, precision: 30) # ALTER TABLE "measurements" ADD "huge_integer" decimal(30) # Defines a column with a database-specific type. add_column(:shapes, :triangle, 'polygon') # ALTER TABLE "shapes" ADD "triangle" polygon
script/generate syntax
To add a post_id field to a comments table, run this:
script\generate migration add_post_id_to_comment post_id:integer
See that it´s not the table name(plural), but the model name(singular),<br /> and post_id:references, does not works like in create_table.
This is the generated migration:
class AddPostIdToComment < ActiveRecord::Migration def self.up add_column :comments, :post_id, :integer end def self.down remove_column :comments, :post_id end end
Options
Available options are (none of these exists by default):
* :limit - Requests a maximum column length. This is number of characters for :string and :text columns and number of bytes for :binary and :integer columns. * :default - The column‘s default value. Use nil for NULL. * :null - Allows or disallows NULL values in the column. This option could have been named :null_allowed. * :precision - Specifies the precision for a :decimal column. * :scale - Specifies the scale for a :decimal column.
:null => false
To not allow a column to have a NULL value, pass :null => false. Seems silly, but that’s it.
Redirect...
See ActiveRecord::ConnectionAdapters::TableDefinition#column for details of the options you can use.
script/generate can take table name
As far as I can tell script/generate will happily take the plural table name, at least in Rails 2.3.