ruby-on-rails - 从 sqlite3 切换到 Postgres 后的关系问题

标签 ruby-on-rails ruby postgresql sqlite

我在尝试访问 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。我可以访问居民和属性(property)的索引页面,我可以创建新属性(property)并对其进行编辑,但不能使用账单。
  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
    

最佳答案

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

关于ruby-on-rails - 从 sqlite3 切换到 Postgres 后的关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196332/

相关文章:

ruby-on-rails - rails : Using form_for multiple times (DOM ids)

html - 添加将 html 转换为 msword 文档的功能

Rubymine - 启用行号

ruby-on-rails - 在 Ruby on Rails 上从多个字段搜索项目名称

ruby-on-rails - 神配置文件监控现有进程?

ruby-on-rails - Ruby on Rails 6 + Docker = Webpacker::Manifest::MissingEntryError?

sql - 比较 PostgreSQL 中的两个数组

postgresql - gradle脚本类路径仅包含脚本类

postgresql - 查询执行时间在没有类型转换的情况下显着增加

ruby-on-rails - 可以将 Apache BalancerMember 配置为使用 unix 域套接字吗?