【问题标题】:"PG::UndefinedColumn:" when deploying rails app部署 Rails 应用程序时出现“PG::UndefinedColumn:”
【发布时间】:2018-04-12 01:58:33
【问题描述】:

使用 Capistrano 将更改部署到生产环境时出现错误

PG::UndefinedColumn: ERROR:  column "address_id" of relation "member_applications" does not exist

在我的迁移中,我有

class CreateAddresses < ActiveRecord::Migration[5.1]
    def change
        create_table :addresses do |t|
            t.string :full_name, null: false
            t.string :email, null: false
            t.string :phone, null: false
            t.string :address, null: false
            t.string :state, null: false
            t.string :country, null: false
            t.string :postcode, null: false
            t.timestamps
        end
    end
end

class CreateMemberApplications < ActiveRecord::Migration[5.1]
    def change
        create_table :member_applications do |t|
            t.references :location, null: false
            t.references :addresses, null: false
            t.references :user, null: false
            t.string :full_name, null: false
            t.date :birthdate, null: false
            t.timestamps
        end
    end
end

最后

class SetAddressToNull < ActiveRecord::Migration[5.1]
  def change
      change_column_null :member_applications, :address_id, true
  end
end

这最后一次迁移似乎是导致问题的原因。什么可能导致此问题?它一直在我的开发机器上运行,但距离我上次部署已经有一段时间了。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql database-migration


    【解决方案1】:

    我相信问题出在这里:

    create_table :member_applications do |t|
      ...
      t.references :addresses, null: false
      ...
    end
    

    应该是单数的t.references :address, null: false

    您可以查看您的db/schema.rb 进行验证。

    【讨论】:

    • 这就是问题所在。不知何故,我的架构使用了正确的 address_id。我永远不会知道这是如何发生的,但是在修复迁移并使用 psql 重命名服务器上的列之后,它现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2011-07-12
    相关资源
    最近更新 更多