change_table(table_name) public

A block for changing columns in table.

Example

# change_table() yields a Table instance
change_table(:suppliers) do |t|
  t.column :name, :string, :limit => 60
  # Other column alterations here
end
Examples
Add a column
change_table(:suppliers) do |t|
  t.column :name, :string, :limit => 60
end
Add 2 integer columns
change_table(:suppliers) do |t|
  t.integer :width, :height, :null => false, :default => 0
end
Add created_at/updated_at columns
change_table(:suppliers) do |t|
  t.timestamps
end
Add a foreign key column
change_table(:suppliers) do |t|
  t.references :company
end

Creates a company_id(integer) column

Add a polymorphic foreign key column
change_table(:suppliers) do |t|
  t.belongs_to :company, :polymorphic => true
end

Creates company_type(varchar) and company_id(integer) columns

Remove a column
change_table(:suppliers) do |t|
  t.remove :company
end
Remove several columns
change_table(:suppliers) do |t|
  t.remove :company_id
  t.remove :width, :height
end
Remove an index
change_table(:suppliers) do |t|
  t.remove_index :company_id
end

See also Table for details on all of the various column transformation

Show source
Register or log in to add new notes.
September 25, 2008
18 thanks

All methods

change_table :table do |t|

  t.column # adds an ordinary column. Ex: t.column(:name, :string)
  t.index # adds a new index.
  t.timestamps
  t.change # changes the column definition. Ex: t.change(:name, :string, :limit => 80)
  t.change_default # changes the column default value.
  t.rename # changes the name of the column.
  t.references
  t.belongs_to
  t.string
  t.text
  t.integer
  t.float
  t.decimal
  t.datetime
  t.timestamp
  t.time
  t.date
  t.binary
  t.boolean
  t.remove
  t.remove_references
  t.remove_belongs_to
  t.remove_index
  t.remove_timestamps
end