PostgreSQL Rails migration – how to fix DatatypeMismatch error

So recently I made my very first blog app (you can watch this awful creation here) and while uploading to Heroku was as expected stress free, migrations weren’t.

The error was

[readolog_blockquote ]

PG::DatatypeMismatch: ERROR:  column “category_id” cannot be cast automatically to type integer

HINT:  Specify a USING expression to perform the conversion.


I could imagine this would cause a problem, because initially “category_id” was named plain “category” and was a string instead of an integer.

By simply creating a new migration

I thought to have it done until I stumbled upon the error.

My first search led me to this awesome article – check it out

(You basically need to follow the HINT and convert your data as following:

However, after adding a new migration… the error still occured. Until I watched carefully the error logs

As you can see, not the new migration is causing problems, but the old one! So if you encounter the same problem as I did, please check your older migrations – you just need to change this line in the old migration as well: