簡體   English   中英

ActiveRecord :: StatementInvalid:Rails中的PG :: UndefinedColumn

[英]ActiveRecord::StatementInvalid: PG::UndefinedColumn in Rails

因此,完整的錯誤是:

失敗/錯誤:@region = Region.find_by!(URL名稱:params [:id])ActiveRecord :: StatementInvalid:PG :: UndefinedColumn:錯誤:列region.url_name不存在第1行:選擇“區域”。* FROM“地區“”地區“。” url_name ...

然后作為一個失敗的例子,我得到:

rspec ./spec/controllers/regions_controller_spec.rb:15#RegionsController show應該渲染顯示頁面

在我的區域控制器中,我需要顯示:

def show
 @region = Region.find_by!(url_name: params[:id])
 @careers = @region.careers
end

對於我的地區規范控制器,我有:

describe 'show' do
it 'should render the show page' do
  region = Region::Create.run(FactoryBot.attributes_for(:region)).result
  get :show, params: { id: region.url_name }
  expect(response).to have_http_status :success
end

it 'should return a 404 error' do
  get :show, params: { id: 1 }
  expect(response).to have_http_status :not_found
end
end

在區域模型中,我有:

class Region < ApplicationRecord
 scope :ordered, -> { order(name: :desc) }
 scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
 has_many :locations, dependent: :destroy
 has_many :careers, through: :locations
 validates :name, presence: true
end

在我的模式中,我有:

create_table "regions", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "url_name"
end

因此該列存在。 該頁面使用url_name加載。 但是,這是錯誤的,並告訴我該列不存在...?

因此,似乎架構中存在url_name,但另一個人刪除了遷移文件。 為了解決這個問題,我運行了刪除列遷移,然后執行了添加列遷移,它解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM