How to change the column type with Rails migrate

How to change the column type with a Rails migration. Lets find out how to do this common Rails task

A common change that you will make to Rails projects is you start with a model having an attribute of one type and then you realise it needs to be another.

This might be something like thinking you should store an integer when it should actually be a string. New developers often think phone numbers should be stored as integers because they are numbers, but forget that integers can’t start with zero (and most phone numbers start with zero).

To kick things off we need to generate a new migration

  rails g migration change_phone_to_be_string_in_customers

What you call this migration will have no impact on what we need to do next, but future you and other developers will thank you for naming your migration appropriately.

If you open up the migration you should see something like this

  class ChangePhoneToBeStringInCustomers < ActiveRecord::Migration[5.0]
    def change
    end
  end

As you can see the change method is sitting empty. We need to manually add some code here.

The code we need to add takes the form of

  change_column :table_name, :column_name, :new_type

In our case it would be something like

  change_column :customers, :phone, :string

The completed file will look like this

class ChangePhoneToBeStringInCustomers < ActiveRecord::Migration[5.0]
  def change
    change_column :customers, :phone, :string
  end
end

Once you’ve saved the file you should be able to do a quick rake db:migrate and your database will update.

Making it reversible

As was kindly pointed out by Ilias in the comments this migration is not reversible, this means you can’t automatically roll it back.

The reason for this is because at no point have we specified the original column attributes so Rails wouldn’t know what to move back to.

The easiest way to fix this is to turn our change method into two methods. up and down.

up lets Rails know what to do when handling a db:migrate and down lets Rails know what to do when handling a db:rollback.

class ChangePhoneToBeStringInCustomers < ActiveRecord::Migration[5.0]
  def up
    change_column :customers, :phone, :string
  end

  def down
    change_column :customers, :phone, :integer
  end
end

This article is a part of the "Rails migrations" series

Recent posts View all

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.

VS Code Web Dev

Select multiple lines in VS Code

How to select multiple lines to edit at once within VS Code