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
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 lets Rails know what to do when handling a
down lets Rails
know what to do when handling a