Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rails: Migrations

Rails: Migrations

Add reference:

# add_reference :table, :column_name, foreign_key: boolean
add_reference :bookings, :flight, foreign_key: true

Add index:

# add_index :table, :column
add_index :requests, :query

# Results in having this line in the table's schema.
t.index ["query"], name: "index_requests_on_query"

Add column:

# add_column :table, :column_name, :column_type
add_column :users, :biography, :text

Rename column:

# rename_column :table, :old_column, :new_column
rename_column :users, :biography, :description

Remove column:

# remove_column :table, :column, :type
remove_column :bookings, :airport, :string

Change column type:

# change_column :table, :column_name, :column_new_type
change_column :flights, :departure_date, :date

Change column null:

# change_column_null :table, :column_name, boolean
change_column_null :shouts, :user_id, false

Add default value to existing column:

# change_column :table, :column, :type, default: something
change_column :things, :price_1, :integer, default: 123

Change default value:

# Rails 5
# change_column_default :table, :column, from: value, to: value
change_column_default :products, :approved, from: true, to: false

# Rails 4.2
# change_column_default :table, :column, value
change_column_default :products, :approved, false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment