【问题标题】:ActiveModel::MissingAttributeError (can't write unknown attribute `order_id`) in productionActiveModel::MissingAttributeError (can't write unknown attribute `order_id`) 在生产中
【发布时间】:2017-03-23 12:19:08
【问题描述】:

在开发环境中一切正常,但在生产环境中(部署到 Heroku 时)它会抛出 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

【问题讨论】:

  • 您是否在 Heroku 上运行过迁移?看来这可能是罪魁祸首。
  • @NickM,我做到了。还重新创建了 heroku 上的数据库 - 也没有用。
  • 您是否将git add .git commit -m "added mig" 迁移到了heroku,然后运行heroku run rake db:migrate
  • 您可以使用heroku run rails dbconsole,然后使用SQL确认该列是否存在
  • @diego.greyrobot,是的

标签: ruby-on-rails ruby postgresql heroku ruby-on-rails-5


【解决方案1】:

当您在 localhost 上迁移时,会构建架构以反映您的本地数据库状态,并且它可能不会与生产环境同步。也许您在迁移中更改了某些内容,但 heroku 仍在使用旧版本。 Heroku 是否运行迁移取决于文件名中的时间戳,而不是迁移的内容。

基本上,如果您有已部署的应用,请不要更改旧迁移。如果您不介意销毁生产数据库中的所有数据,请运行 heroku pg:reset DATABASE 然后 rake db:migrate again。如果无法删除数据,仍有办法解决问题 - 见Rails rake db:migrate has no effect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多