Heroku Migration Fails to Update Attributes: reset_column_information
On several occasions I’ve had Heroku apps fail to recognize columns I add to the database. The symptoms of this problem are:
- The app throws an error if you visit a page with the new methods referencing the column names
- If you use heroku console and create your new object it won’t have the attributes for your new column.
This happened to me yesterday and here is how to fix it. First, I made sure that the migration had taken effect. I used the handy heroku sql console plugin which allows you to execute sql on your Heroku database. This allowed me to checkout the schema_migrations table and make sure my latest migration was there:
[code]SELECT * FROM schema_migrations;[/code]
Then I looked at the table with the added columns to see if it had the changes:
[code]DESCRIBE courses;[/code]
The columns were there.
The reset_column_information method turned out to be the key to fixing this problem:
[code]
heroku console
>> Course.reset_column_information
[/code]
Now Course had the recently added attributes. To avoid this problem next time, I’ll use reset_column_information inside of the migration when I change an existing model’s attributes. An additional facet to this solution is that if you access a model class in a migration, you need to open up the class in the migration:
[code]
class Course < ActiveRecord::Base; end
[/code]
Here are some resources:
6 Comments
Thanks so much was having the same problem and this solved it!
I encountered the same weird problem. Thanks for pointing out this solution. It worked like a charm!
Wow! Perfect timing. I wasn’t able to figure out how to do this by reading Heroku’s site. You just saved me a ton of time, thanks!
Thanks, was messing around now for an hour to figure out what’s happening.
Thank you! Just ran into this bizarre issue with Heroku this morning. Oddest errors were popping up, but I definitely narrowed it down to the new columns. This will really help (hopefully) preventing this in the future. Now I’m wondering if it’s a bug with Heroku or Postgres… Any thoughts?
Thanks man. Much appreciated tip :). This was happening to me locally as well.