【问题标题】:A relation issue after switching from sqlite3 to Postgres从 sqlite3 切换到 Postgres 后的关系问题
【发布时间】:2016-01-16 17:35:01
【问题描述】:

尝试访问 index#bills 页面时出现此错误:

ActiveRecord::StatementInvalid in BillsController#index
PG::UndefinedTable: ERROR: relation "bills" does not exist LINE 5: 
WHERE a.attrelid = '"bills"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), 
a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN 
pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE 
a.attrelid = '"bills"'::regclass AND a.attnum > 0 AND NOT a.attisdropped 
ORDER BY a.attnum

我按照本指南了解如何从 Sqlite3 切换到 Postgres: http://www.daveferrara1.com/ruby-in-rails-switch-from-sqlite3-to-postgres/

以下是有关我的应用的一些信息:

  1. 我有三个模型,Property、bill 和 resident。我可以访问居民和财产的索引页面,我可以创建新财产并对其进行编辑,但不能使用账单。
  2. 切换后出现此问题。在切换到 Postgres 之前它运行良好。
  3. 这就是我的 database.yml 的样子:

    development:
      adapter: postgresql
      encoding: unicode
      database: demo_test_development
      pool: 5
      username: 
      password:
      timeout: 5000
    
    test:
      adapter: postgresql
      encoding: unicode
      database: demo_test_test
      pool: 5
      username: 
      password:
      timeout: 5000
    
    production:
      adapter: postgresql
      encoding: utf8
      database: demo_test_production
      pool: 5
      username: 
      password:
    
  4. 我的模型:

    class Bill < ActiveRecord::Base
      belongs_to :property
      belongs_to :user
      belongs_to :resident
      has_many :photos
    end
    
    class Property < ActiveRecord::Base
      belongs_to :user
      has_many :photos
      has_one :resident
      has_many :bills    
      validate :address , :city , :country
    end
    
    class Resident < ActiveRecord::Base
        belongs_to :user
        has_many :photos
        has_one :property
        has_many :bills  
        validates_presence_of :name
    end
    
  5. 这是我的 BillsController 显示错误的地方:

    class BillsController < ApplicationController
      before_action :set_bill, only: [:show, :edit, :update]
      before_action :authenticate_user!
    
      def index
        @bills = current_user.bills
      end
    
      def show
        @bill = Bill.find(params[:id])
        @photos = @bill.photos
    
      end
    
      def new
        @bill = current_user.bills.build
      end
    
      def create
        @bill = current_user.bills.build(bill_params)
        if @bill.save
          if params[:images]
            params[:images].each do |image|
              @bill.photos.create(image: image)
            end
          end
    
          @photos = @bill.photos
          redirect_to edit_bill_path(@bill), notice: "Saved"
        else
          render :new
        end
      end
    
      def edit
        if current_user.id == @bill.user.id
          @photos = @bill.photos
        else
          redirect_to root_path, notice: "You don't have access to this page"
        end
      end
    
      def update
        if @bill.update(bill_params)
          if params[:images]
            params[:images].each do |image|
              @bill.photos.create(image: image)
            end
          end
    
          redirect_to edit_bill_path(@bill), notice: "Updated"
        else
          render :edit
        end
      end
    
      private
    
      def set_bill
        @bill = Bill.find(params[:id])
      end
    
      def bill_params
        params.require(:bill).permit(:due_date, :amount, :bill_type, :bill_status, :description, :property_id, :resident_id)
      end
    end
    

【问题讨论】:

  • 错误信息表示PostgreSQL没有找到名为“bills”的表。看看PostgreSQL是否正确。
  • @MikeSherrill'CatRecall' 我可以看到 schema.rb 中的表,在转换到 Posgres 之前或之后我没有更改数据库中的任何内容。它在使用 Sqlite 之前没有任何问题。
  • PostgreSQL 不在乎 schema.rb 中有什么。数据库中的表是“账单”吗?错误消息说不是。
  • @MikeSherrill'CatRecall' 这是检查数据库后返回的内容:demo_test_development=# \dt List of relations Schema | Name | Type | Owner --------+-------------------+-------+------- public | Bills | table | alioh --- public | photos | table | alioh --- public | properties | table | alioh --- public | residents | table | alioh --- public | schema_migrations | table | alioh --- public | users | table | alioh
  • 您似乎有一个名为“Bills”的表,而不是“bills”。

标签: ruby-on-rails ruby postgresql sqlite


【解决方案1】:

表名是“Bills”,但您的查询正在查找“bills”。那是不同的名字!

【讨论】:

  • 修复了它,这是否意味着 Posgres 区分大小写?因为我没有得到这个表与 Sqlite3 的错误。
  • 视情况而定。如果您引用表名,那么是的,它区分大小写。如果您不引用表名,那么 PostgreSQL 会将它们设为小写,但不关心您是否指定它们是大写、小写或混合大小写。决定你想要的方式,引用而不是引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 2015-10-06
  • 2021-01-04
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多