dimanche 18 octobre 2015

A relation issue after switching from sqlite3 to Postgres

I get this error when trying to access index#bills page:

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

I followed this guide on how to switch from Sqlite3 to Postgres: http://ift.tt/1hLgdWz

Here are some info about my app:

  1. I have three models, Property, bill and resident. I can access the index page of both resident and property and I can create new property and edit it but not with bills.
  2. this issue appeared after the switching. It was working fine before switching to Postgres.
  3. this is how my database.yml look like:

    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. my models:

    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. and this is my BillsController where it show the error:

    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
    
    

Aucun commentaire:

Enregistrer un commentaire