简体   繁体   中英

ActiveRecord::StatementInvalid: PG::UndefinedColumn in Rails

So the full error is:

Failure/Error: @region = Region.find_by!(url_name: params[:id]) ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column regions.url_name does not exist Line 1: SELECT "regions".* FROM "regions" WHERE "regions"."url_name...

Then as a failed example I get:

rspec ./spec/controllers/regions_controller_spec.rb:15 # RegionsController show should render the show page

In my regions controller I have for show:

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

For my regions spec controller I have:

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

In the model for regions I have:

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

And in my schema I have:

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

So the column exists. The page loads using the url_name. It's however erroring and telling me that the column doesn't exist...?

So it looks as though url_name was present in the schema but another person removed the migration file. To resolve I ran a remove column migration then did an add column migration and it resolved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM