3

I'm trying to map an existing PostgreSQL view (which creation script was ran and tested in a different computer) to a Rails application, but I keep getting PG::UndefinedTable: ERROR: relation "reports_consultants_clients_tasks" does not exist.

First, this is my routes file:

Rails.application.routes.draw do
  namespace :reports do
  get 'consultants_clients_tasks/index'
  end
  # rest of the file
end

The controller:

class Reports::ConsultantsClientsTasksController < ApplicationController
  def index
    @consultants_clients_tasks = ConsultantsClientsTask.all
  end
end

The model (I tried with and without table name, anyway, it should be following the convention... right?)

class Reports::ConsultantsClientsTask < ActiveRecord::Base
  #self.table_name = "CONSULTANTS_CLIENTS_TASKS"
end

This is my migration to create the view:

class CreateReportsConsultantsClientsTasksSqlView < ActiveRecord::Migration
  def change
    execute <<-SQL
      CREATE VIEW CONSULTANTS_CLIENTS_TASKS AS
      -- rest of the script
      ;
    SQL
  end
end

I tried running both rake db:reset or rake db:drop db:create db:migrate and in both cases the view isn't properly created.

If I run this (the query generated by ActiveRecord), from a psql console:

SELECT "CONSULTANTS_CLIENTS_TASKS".* FROM "CONSULTANTS_CLIENTS_TASKS";

I get this error:

ERROR:  relation "CONSULTANTS_CLIENTS_TASKS" does not exist
LINE 1: SELECT "CONSULTANTS_CLIENTS_TASKS".* FROM "CONSULTANTS_CLIEN...

However, running this other query:

select * from CONSULTANTS_CLIENTS_TASKS;

gets no errors:

consultant_name | client_name | task_name | task_date | task_duration | consultant_cost | client_amount | task_cost_related -----------------+-------------+-----------+-----------+---------------+-----------------+---------------+------------------- (0 rows)

I haven't mapped all the view's fields in my .html.erb but I don't think that's a problem.

So, any ideas on why Rails isn't able to query properly the View? Or why isn't the view properly created?
Thanks in advance

3
  • 2
    Shouldn't it be lowercase consultants_clients_tasks? Commented Apr 15, 2016 at 19:40
  • Please post thsi as an answer and I'll accept it. That was the issue Commented Apr 15, 2016 at 19:55
  • 2
    PostgreSQL folds unquoted identifiers to lower case, Rails will quote identifiers behind your back. Your view is called consultants_clients_tasks but your code is looking for "CONSULTANTS_CLIENTS_TASKS". Commented Apr 15, 2016 at 19:56

2 Answers 2

3

Shouldn't it be lowercase: consultants_clients_tasks?

Sign up to request clarification or add additional context in comments.

Comments

0

PG::UndefinedTable: ERROR: relation "reports_consultants_clients_tasks" does not exist.

The error messages seems to suggest that database is missing the table for reports_consultants_clients_tasks. Check db/schemas.rb and see if you have "reports_consultants_clients_tasks"

If you don't maybe you need to migrate the database

rake db:migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.