I’ve got a one-off script that I’m using to load some data into my application’s database.
This is kind of large-ish so I’ve gist’d it here: https://gist.github.com/097fb5a30ce84522077a
The code as written works flawlessly when executing against a local Postgres instance on my local machine (or even a remote instance) as specified by the DATABASE_URL environment variable.
However, it will absolutely not work from Heroku. Executing the ruby script linked gives me this error:
~ $ ruby importprofiles.rb
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes': unknown attribute: owner_id
(ActiveRecord::UnknownAttributeError)
My workflow is basically:
- Create the app on Heroku (
heroku create mybot) - Push the code up (
git commit -m 'initial commit' && git push heroku master) - Set up the database (
heroku run rake db:setup) - Restart the instance (
heroku restart) - Invoke a Bash shell on the heroku instance (
heroku run bash) - Use Curl to pull down my production profiles.ini from a server (
curl http://blah.com/profiles.ini > profiles.ini) - Run the script (
ruby importprofiles.rb)
Why am I getting unknown attribute errors on Heroku for code that works locally?
Your
schema.rbdoesn’t match your real database or you’ve never tried to use aProfileowner for anything. You have this inschema.rb:The
t.integer "owner"defines a column namedownerin theprofilestable but you need anowner_idcolumn.Perhaps you did a manual
alter tableto rename the column in your development environment. Or maybe this is the first time you’ve gone along this code path.In any case, you need a migration to rename
profiles.ownertoprofiles.owner_id. Or, since you’re just getting started, you could hand-editschema.rbto rename the column and the rebuild your Heroku database. I’d just patchschema.rbby hand and start again if you don’t have any data to worry about.