首页 > 解决方案 > PG::UndefinedTable: 错误: 关系 "cities" 不存在第 8 行: WHERE a.attrelid = '"cities"'::regclass ^

问题描述

我是 Sinatra 的新手,并试图创建一个发布请求,该请求会将一些数据添加到 PGadmin 的“城市”表中。但是,尝试添加它时出现错误。获取请求工作正常,在 post 端点上发布静态数据也工作正常,但发布数据不是我不确定出了什么问题,因为迁移和城市模型确实存在。这是代码:

城市.rb

class City < UserDB
    validates_presence_of :name, :pin


    def as_json(options = {})
        super({only: [:city, :pin]})
    end
end

移民:

class CreateCity < ActiveRecord::Migration[6.0]
  def change
    create_table :city do |t|
      t.string :name
      t.integer :pin
    end
  end
end


module Users
    module V1
        class CityController< LocalAPI::ApplicationBase
            before do
                content_type 'application/json'
            end
            get '/' do
                city=City.all
                if city
                    "city exists"
                else
                    "doesn't exist"
                end
            end
           
            post '/add' do
                params = JSON.parse request.body.read
                @name= params['name']
                @pin= params['pin']
                @city=City.new(name:@name,pin:@pin)
                
                if @city.save
                    return output(200, @city.as_json)
                else 
                    "cant create"
                end
            end
            
           

        end
    end
 end

我正在尝试通过邮递员发布以下内容:

{
    "name": "hamilton",
    "pin":"222"
}

请注意,路由已配置,当我访问 url 时它确实有效。也跑过db:migrate

标签: rubypostgresqlsinatrarails-activerecord

解决方案


推荐阅读