7

I added a table to my local env called schools and it works fine in dev. In fact it even works fine in staging (heroku) but fails big time in production and rake db:migrate throws the error bellow.

I cannot even precompile assest (with RAILS_ENV=production), access any part of my application including the rails console in production(heroku). Everything is throwing the error bellow. I have lost several hours on this but do not feel any closer to figuring it our. (Note: I have '"..."' on the reference to the table_name which I think is part of the problem)

I went from rails 3.1.0 to 3.1.3 so that might have something to do with it.

PGError: ERROR:  relation "schools" does not exist

LINE 4:              WHERE a.attrelid = '"schools"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"schools"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

I am as lost as can be on this. I read several other questions on this topic here but no solution at sight. Thank you for your help. Any insight is greatly appreciated.

UPDATE------------------------------------------------------

I just created a brand new app on heroku and ran rake db:migrate and got the same error.

UPDATE 2

I cloned the app from heroku and "schools" is in the schema.

create_table "schools", :force => true do |t|
  ...
  ...
end

UPDATE 3

Tried reverting back to rails 3.1.0 but that did not help.

UPDATE 4

Still working on this. Have not heard from heroku support yet.

UPDATE 5

Heroku support was able to check that 'schools' is not a table in the DB but I still cannot access the console and app still down.

Yuri
  • 1,261
  • 1
  • 11
  • 23
  • Did you run a migration in production to create the `schools` table? That query looks like ActiveRecord trying to figure out the structure of `schools` for the `School` model. – mu is too short Jan 09 '12 at 07:08
  • thanks for responding mu is too short. I did run the migration but it gives me that error. Strangely enough though, the migrations seems to have completed properly as I cloned the app from production and `schools` is in the schema. – Yuri Jan 09 '12 at 08:25
  • But is the `schools` table in the database? What happens if you try to pull down a copy of your production database? Does it contain a `schools` table? – mu is too short Jan 09 '12 at 09:01
  • I cannot pull it [using taps/db:pull](http://devcenter.heroku.com/articles/taps) and I cannot access the console (get the error above). Are there other ways of assuring the table status? – Yuri Jan 09 '12 at 09:46
  • 1
    [Over here](http://stackoverflow.com/questions/5450930/heroku-postgres-error-pgerror-error-relation-organizations-does-not-exist) they seem to have gone for the "kill off and rebuild the app approach". That's hardly a solution in my book but something is FUBARed. Any application initializers or non-standard things? Have you tried deleting your `models/school.rb` to see what happens? – mu is too short Jan 09 '12 at 09:56
  • I tried creating a new app, while leaving main untouched, and deploying. That did not solve it either. I am reluctant to destroy my current live one but I was able to backup the db recently so I guess that is an option. I put a ticket in to heroku support. Hopefully they will be able to help. – Yuri Jan 09 '12 at 10:11
  • Thank you for your insight and assistance mu is too short. I will make sure to post a solution here once I find one. – Yuri Jan 09 '12 at 10:35
  • 2
    Looks like ActiveAdmin might be causing the problem ? – Thong Kuah Jan 09 '12 at 11:01
  • Thanks Thong Kuah but it does not look like it is ActiveAdmin either. – Yuri Jan 09 '12 at 18:09
  • Ok, confirmed that 'schools' table is not in the DB. Is creating a second schools table via migration something viable? Since the problems seems mainly executing my current schools table migration file. – Yuri Jan 09 '12 at 20:43
  • Thong, I am not sure what you meant when you but you where in the right Direction. It was ActiveAdmin. See final update. – Yuri Jan 09 '12 at 21:53
  • done. Will accept when as soon as allowed (in 22 hours from now). – Yuri Jan 10 '12 at 07:52

4 Answers4

6

Heroku Support helped me solve this even though it was not necessarily heroku's platform related issue as we found out.

I was having a chicken-and-egg issue, where the migration wouldn't run because the rails bootstrap was trying to talk to the table and the table was not created because migration could not run. ActiveAdmin was initializing when rails boots, and was trying to look up schools. Thank you everyone that helped.

Yuri
  • 1,261
  • 1
  • 11
  • 23
  • 5
    Thanks for mentioning **ActiveAdmin**. `rake db:reset RAILS_ENV=test` returned the same PGError you reported. Though I could not find the exact cause I 1. removed my `initializers/active_admin.rb`, 2. Moved `app/admin` out of the project, and 3. Commented out `ActiveAdmin.routes(self)` in `config/routes.rb`. `rake db:reset RAILS_ENV=test` then ran without trouble. I then reverted the above 3 steps and was on my way. – deefour Feb 22 '12 at 16:44
5

I've solved it by commenting

#ActiveAdmin.routes(self)

in routes before migration. After completing of migration i've just uncomments this line and everything work fine.

1

No, the quoting should not be a problem. While the double quotes are not needed in this case, they are not wrong, either:

SELECT '"schools"'::regclass

I would consider that PostgreSQL is right and the relation schools does no exist - in your database and in a schema that is part of your search_path.

If you can connect to your database, you could run this query to diagnose your problem:

SELECT n.nspname AS schema_name
      ,c.relname AS table_name
      ,c.relhastriggers
      ,c.reltuples
FROM   pg_catalog.pg_class c
LEFT   JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relname ~~* '%schools%'
AND    c.relkind = 'r'
AND    nspname <> 'pg_catalog';

Finds every table where the name contains the string schools. Similar to the psql meta-command:

\dt *schools*

Make sure you are connected with the same user to the same database where you have the problem.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Thank you for responding Erwin. What would you suggest I should do? I have the migration `class CreateSchools < ActiveRecord::Migration def change create_table :schools do |t|` and it works on both local and staging env. How would you suggest I trouble shoot it etc? – Yuri Jan 09 '12 at 07:04
  • I cannot run `rails console`, `heroku run console` everything gives me the error above. I am pretty badly blocked out of the app on production. – Yuri Jan 09 '12 at 07:18
  • Cloned the app. School is in the schema. Am I missing something more obvious here? – Yuri Jan 09 '12 at 07:25
  • 1
    @Yuri: Is it "School" or "school". When double-quoted, case is relevant. Also, in your example above, you had **"schools"**, plural. – Erwin Brandstetter Jan 09 '12 at 08:11
  • `school`. I pasted the schema code for school from the clone on the main question. Thank you for responding. – Yuri Jan 09 '12 at 08:23
  • Rails would want the table to be called `schools` and the class name would be `School`; the query in question is comes from Rails (hence all the quoting) trying to figure out the schema of the `schools` table so that it can sort out the `School` class. – mu is too short Jan 09 '12 at 09:49
  • I see. Sorry about the confusion. In that case: model is `ActiveAdmin.register School do...` and table is `schools` – Yuri Jan 09 '12 at 09:57
  • and `class School < ActiveRecord::Base` – Yuri Jan 09 '12 at 17:24
0

I came across this issue when running tests.

Simply reset the database for test environment:

rake db:reset RAILS_ENV=test