up
and down
that describes the transformations required to implement or remove the migration. These methods can consist of both the migration specific methods like add_column
and remove_column
, but may also contain regular Ruby code for generating data needed for the transformations.system_settings
table, then creates the very first row in it using the Active Record model that relies on the table. It also uses the more advanced create_table
syntax where you can specify a complete table schema in one block call.create_join_table(table_1, table_2, options)
: Creates a join table having its name as the lexical order of the first two arguments. See ActiveRecord::ConnectionAdapters::SchemaStatements#create_join_table
for details.create_table(name, options)
: Creates a table called name
and makes the table object available to a block that can then add columns to it, following the same format as add_column
. See example above. The options hash is for fragments like “DEFAULT CHARSET=UTF-8” that are appended to the create table definition.add_column(table_name, column_name, type, options)
: Adds a new column to the table called table_name
named column_name
specified to be one of the following types: :string
, :text
, :integer
, :float
, :decimal
, :datetime
, :timestamp
, :time
, :date
, :binary
, :boolean
. A default value can be specified by passing an options
hash like { default: 11 }
. Other options include :limit
and :null
(e.g. { limit: 50, null: false }
) – see ActiveRecord::ConnectionAdapters::TableDefinition#column
for details.add_foreign_key(from_table, to_table, options)
: Adds a new foreign key. from_table
is the table with the key column, to_table
contains the referenced primary key.add_index(table_name, column_names, options)
: Adds a new index with the name of the column. Other options include :name
, :unique
(e.g. { name: 'users_name_index', unique: true }
) and :order
(e.g. { order: { name: :desc } }
).add_reference(:table_name, :reference_name)
: Adds a new column reference_name_id
by default an integer. See ActiveRecord::ConnectionAdapters::SchemaStatements#add_reference
for details.add_timestamps(table_name, options)
: Adds timestamps (created_at
and updated_at
) columns to table_name
.change_column(table_name, column_name, type, options)
: Changes the column to a different type using the same parameters as add_column.change_column_default(table_name, column_name, default_or_changes)
: Sets a default value for column_name
defined by default_or_changes
on table_name
. Passing a hash containing :from
and :to
as default_or_changes
will make this change reversible in the migration.change_column_null(table_name, column_name, null, default = nil)
: Sets or removes a +NOT NULL+ constraint on column_name
. The null
flag indicates whether the value can be NULL
. See ActiveRecord::ConnectionAdapters::SchemaStatements#change_column_null
for details.change_table(name, options)
: Allows to make column alterations to the table called name
. It makes the table object available to a block that can then add/remove columns, indexes or foreign keys to it.rename_column(table_name, column_name, new_column_name)
: Renames a column but keeps the type and content.rename_index(table_name, old_name, new_name)
: Renames an index.rename_table(old_name, new_name)
: Renames the table called old_name
to new_name
.drop_table(name)
: Drops the table called name
.drop_join_table(table_1, table_2, options)
: Drops the join table specified by the given arguments.remove_column(table_name, column_name, type, options)
: Removes the column named column_name
from the table called table_name
.remove_columns(table_name, *column_names)
: Removes the given columns from the table definition.remove_foreign_key(from_table, to_table = nil, **options)
: Removes the given foreign key from the table called table_name
.remove_index(table_name, column: column_names)
: Removes the index specified by column_names
.remove_index(table_name, name: index_name)
: Removes the index specified by index_name
.remove_reference(table_name, ref_name, options)
: Removes the reference(s) on table_name
specified by ref_name
.remove_timestamps(table_name, options)
: Removes the timestamp columns (created_at
and updated_at
) from the table definition.ActiveRecord::IrreversibleMigration
exception in their down
method. Ssh rsa key.timestamp_my_new_migration.rb
in the db/migrate/
directory where timestamp
is the UTC formatted date and time that the migration was generated.timestamp_add_fieldname_to_tablename.rb
, which will look like this:rails db:migrate
. This will update the database by running all of the pending migrations, creating the schema_migrations
table (see “About the schema_migrations table” section below) if missing. It will also invoke the db:schema:dump command, which will update your db/schema.rb file to match the structure of your database.rails db:rollback VERSION=X
where X
is the version to which you wish to downgrade. Alternatively, you can also use the STEP option if you wish to rollback last few migrations. rails db:rollback STEP=2
will rollback the latest two migrations.ActiveRecord::IrreversibleMigration
exception, that step will fail and you'll have some manual work to do.Base#reset_column_information
in order to ensure that the model has the latest column data from after the new column was added. Example:say_with_time
method:down
for you. You simply supply the up
logic, and the Migration
system figures out how to execute the down commands for you.change
method in your migration like this:up
and down
methods as before.ActiveRecord::IrreversibleMigration
exception will be raised when the migration is moving down.ActiveRecord::Migration::CommandRecorder
.Migration
with self.disable_ddl_transaction!
.[RW] | name |
[RW] | version |
ActiveRecord::PendingMigrationError
error if any migrations are pending.up
and down
of the yielded object to run a block only in one given direction. The whole block will be called in the right order within the migration.TenderloveMigration
is defined as in the documentation for Migration:published
will be given the value true
for all existing records.has_many
and belongs_to
associations, and I want to take a moment to show you a couple more of them.references
column type in migrations instead.bin/rails generate migration AddPostToComments post:references
add_reference
method instead of add_column
. add_reference
takes a symbol with a table name, and a symbol with the name of a model to add a foreign key for. It'll create a column whose name begins with that model name, and ends in _id
. And since it's always desirable to have an index on foreign key columns, add_reference
will add an index as well. So add_reference :comments, :post
will create a post_id
column in the comments
table just like add_column
did, but it will also add an index on that column automatically.foreign_key: true
argument will set up a foreign key constraint on databases that support it.post_id
field set to 999
, even if there was no record in the post
table with an id
of 999
. With a foreign key constraint, the database would prevent such a record from even being saved. Foreign key constraints help keep bad data from sneaking into your database.Comment
model. We can replace the two migrations with a single migration that creates the comments
table and adds a post_id
column along with the other columns: rails g model Comment content:text name:string post:references
.bin/rails db:migrate
.