That migration will create a team_id column in the users table.
If you want to add an appropriate index and foreign_key on the added column, change the command to rails generate migration AddTeamRefToUsers team:references:index. This will generate the following migration:
If you want to name your reference column other than what Rails auto generates, add the following to your migration: (E.g.: You might want to call the User who created the Post as Author in the Post table)
To run a specific migration up or down, use db:migrate:up or db:migrate:down.
Up a specific migration:
rake db:migrate:upVERSION=20090408054555
rails db:migrate:upVERSION=20090408054555
Down a specific migration:
rake db:migrate:downVERSION=20090408054555
rails db:migrate:downVERSION=20090408054555
The version number in the above commands is the numeric prefix in the migration’s filename. For example, to migrate to the migration 20160515085959_add_name_to_users.rb, you would use 20160515085959 as the version number.
Redo migrations
You can rollback and then migrate again using the redo command. This is basically a shortcut that combines rollback and migrate tasks.
Run command:
rake db:migrate:redo
rails db:migrate:redo
You can use the STEP parameter to go back more than one version.
For example, to go back 3 migrations:
rake db:migrate:redoSTEP=3
rails db:migrate:redoSTEP=3
Add a new column to a table
To add a new column name to the users table, run the command:
When the migration name is of the form AddXXXToTABLE_NAME followed by list of columns with data types, the generated migration will contain the appropriate add_column statements.
Remove an existing column from a table
To remove existing column name from users table, run the command:
When the migration name is of the form RemoveXXXFromYYY followed by list of columns with data types then the generated migration will contain the appropriate remove_column statements.
While it’s not required to specify the data type (e.g. :string) as a parameter to remove_column, it is highly recommended. If the data type is not specified, then the migration will not be reversible.
Running migrations in different environments
To run migrations in the test environment, run this shell command:
rake db:migrateRAILS_ENV=test
Starting in Rails 5.0, you can use rails instead of rake:
rails db:migrateRAILS_ENV=test
Create a new table
To create a new users table with the columns name and salary, run the command:
When the migration name is of the form CreateXXX followed by list of columns with data types, then a migration will be generated that creates the table XXX with the listed columns.
Running migrations
Run command:
rake db:migrate
rails db:migrate
Specifying target version will run the required migrations (up, down, change) until it has reached the specified version. Here, version number is the numerical prefix on the migration's filename.
rake db:migrateVERSION=20080906120000
rails db:migrateVERSION=20080906120000
Change an existing column’s type
To modify an existing column in Rails with a migration, run the following command:
rails g migration change_column_in_table
This will create a new migration file in db/migration directory (if it doesn’t exist already), which will contain the file prefixed with timestamp and migration file name which contains the below content:
The above code prevents the user from ever rolling back the migration. You can avoid this problem by splitting the change method into separate up and down methods:
Migrations with defaults might take a long time in large tables with for example PostgreSQL. This is because each row will have to be updated with the default value for the newly added column. To circumvent this (and reduce downtime during deployments), you can split your migration into three steps:
Add a add_column-migration similar to the one above, but set no default
Deploy and update the column in a rake task or on the console while your app is running. Make sure your application already writes data to that colum for new/updated rows.
Add another change_column migration, which then changes the default of that column to the desired default value
Create a join table
To create a join table between students and courses, run the command:
$ rails g migration CreateJoinTableStudentCourse student course
The foreign key column will be pages_id. If you want to decide about the foreign key column name, you have to create the column first and add the reference after.
If you have created a table with some wrong schema, then the easiest way to change the columns and their properties is change_table. Review the following example:
change_table :ordersdo|t|
t.remove :ordered_at# removes column ordered_at
t.string :skew_number# adds a new column
t.index :skew_number#creates an index
t.rename :location,:state#renames location column to stateend
The above migration changes a table orders. Here is a line-by-line description of the changes:
t.remove :ordered_at removes the column ordered_at from the table orders.
t.string :skew_number adds a new string-type column named skew_number in the orders table.
t.index :skew_number adds an index on the skew_number column in the orders table.
t.rename :location, :state renames the location column in the orders table to state.
Add an unique column to a table
To add a new unique column email to users, run the following command:
StatusMigrationIDMigrationName--------------------------------------------------
up 20140711185212Create documentation pages
up 20140724111844Create nifty attachments table
up 20140724114255Create documentation screenshots
up 20160213170731Create owners
up 20160218214551Create users
up 20160221162159**********NOFILE**********
up 20160222231219**********NOFILE**********
Under the status field, up means the migration has been run and down means that we need to run the migration.
Forbid null values
To forbid null values in your table columns, add the :null parameter to your migration, like this:
Say you want to add a foreign key company_id to the users table, and you want to have a NOT NULL constraint on it. If you already have data in users, you will have to do this in multiple steps.
classAddCompanyIdToUsers<ActiveRecord::Migrationdefup# add the column with NULL allowed
add_column :users,:company_id,:integer# make sure every row has a valueUser.find_each do|user|# find the appropriate company record for the user# according to your business logic
company =Company.first
user.update!(company_id: company.id)end# add NOT NULL constraint
change_column_null :users,:company_id,falseend# Migrations that manipulate data must use up/down instead of changedefdown
remove_column :users,:company_idendend
Parameters
Column type
Description
:primary_key
Primary key
:string
Shorter string datatype. Allows limit option for maximum number of characters.
:text
Longer amount of text. Allows limit option for maximum number of bytes.
:integer
Integer. Allows limit option for maximum number of bytes.
:bigint
Larger integer
:float
Float
:decimal
Decimal number with variable precision. Allows precision and scale options.
:numeric
Allows precision and scale options.
:datetime
DateTime object for dates/times.
:time
Time object for times.
:date
Date object for dates.
:binary
Binary data. Allows limit option for maximum number of bytes.
:boolean
Boolean
Remarks
Most migration files live in `db/migrate/` directory. They’re identified by a UTC timestamp at the beginning of their file name: `YYYYMMDDHHMMSS_create_products.rb`.
The `rails generate` command can be shortened to `rails g`.
If a `:type` is not passed to a field, it defaults to a string.