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
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
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
In our case it would be something like
The completed file will look like this
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
.
This article is a part of the "Rails migrations" series
- Rails Migrations for Beginners
- This Article
- Removing fields with a Rails migration
- Running Rails migrations automatically on Heroku
- How to comment Rails migrations
- Create or remove columns or tables with Rails migrations
- Rails migrations - add default value to existing column
- What are the square brackets in my Rails migrations?
- What are Rails Migrations
- Forcing a Rails database column to be not null
- Irreversible Rails Migrations