0

In development environment everything works fine but in production (when deployed to Heroku) it throws me MissingAttributeError.

class Order < ApplicationRecord
  has_many :cart_items, dependent: :destroy
end

class CartItem < ApplicationRecord
  belongs_to :order, optional: true, foreign_key: "order_id"
end

create_table "cart_items", force: :cascade do |t|
  t.integer  "item_id"
  t.integer  "cart_id"
  t.integer  "user_id"
  t.integer  "order_id"
end
  • 1
    Have you run your migrations on Heroku? Seems that could be the culprit. – NM Pennypacker Nov 09 '16 at 16:07
  • @NickM, I did. Also recreated the db on heroku - didn't work either. – Roman Kanafotskiy Nov 09 '16 at 16:09
  • Did you `git add .` and `git commit -m "added mig"` your migration so that it can be pushed to heroku and then run `heroku run rake db:migrate` ? – DiegoSalazar Nov 09 '16 at 18:32
  • You can use `heroku run rails dbconsole` and then use SQL to confirm the column is there – max pleaner Nov 10 '16 at 06:51
  • @diego.greyrobot, yup – Roman Kanafotskiy Nov 11 '16 at 09:05
  • @maxpleaner, hmm. It turned out it's not there. But how can the column be missing if it's on the local db schema pushed to github and deployed to heroku? – Roman Kanafotskiy Nov 13 '16 at 15:21
  • when you migrate on localhost, the schema is built to reflect your local db state and it's possible for it to be un-synced from production. Perhaps you've changed something in a migration, but heroku is still working off the old version. Whether or not Heroku runs a migration is determied by the timestamp in the filename, not the content of the migration. Basically, if you have a deployed app, don't change old migrations. If you don't mind destroying all the data in the production database, run `heroku pg:reset DATABASE` then `rake db:migrate` again. – max pleaner Nov 13 '16 at 17:03
  • @maxpleaner, wow, thanks! It worked perfectly. Can you write it as the answer so I can mark it as an answered question? – Roman Kanafotskiy Nov 14 '16 at 12:18

1 Answers1

1

when you migrate on localhost, the schema is built to reflect your local db state and it's possible for it to be un-synced from production. Perhaps you've changed something in a migration, but heroku is still working off the old version. Whether or not Heroku runs a migration is determied by the timestamp in the filename, not the content of the migration.

Basically, if you have a deployed app, don't change old migrations. If you don't mind destroying all the data in the production database, run heroku pg:reset DATABASE then rake db:migrate again. If you can't delete the data, there are still ways to fix the problem - see Rails rake db:migrate has no effect

Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118