44
votes

I'm having a problem deploying my Rails app to Heroku, where this error is thrown when trying to access the app:

PGError: ERROR: relation "organizations" does not exist (ActiveRecord::StatementInvalid)

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 = '"organizations"'::regclass
  AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum

Anybody have any ideas? This is a first for me, especially because I've been working with Heroku for a year on other apps, and haven't see anything like this. Of course, everything works on local SQLite.

13
The organizations table doesn't seem to exist. did you run your migration? - Shreyas
Thanks @shreyas, yes, the migration was run, and this is the output: == CreateOrganizations: migrating ============================================ -- create_table(:organizations) -> 0.0175s -- add_index(:organizations, [:organization_type], {:name=>"organizations_index"}) -> 0.0054s == CreateOrganizations: migrated (0.0238s) =================================== - Mark
Also, when I log into the console and check: >> ActiveRecord::Base.connection.tables => ["schema_migrations", "jobs", "organizations", etc... - Mark
Now that you have your organizations table created, are you still facing a problem? - Shreyas
Yes, the organizations table was already created when the problem occurred, thanks... - Mark

13 Answers

82
votes

I had the same problem. To solve it, resetting the database is more easier.

  • heroku rake db:reset ('heroku run rake db:reset' if you're on cedar)
  • heroku rake db:migrate ('heroku run rake db:migrate' if you're on cedar)

Then, migration was done successfully for my case :)

While this is a good solution in this context, don't do it on production. It will delete all the records from your database

21
votes

I've had the same problem until I realized that I had to do:

heroku rake db:migrate

:)

19
votes

According to my experience (Rails 3.1, Sedar stack) after running pg:reset and db:migrate you might have to run heroku restart.

7
votes

My heroku version:

heroku --version
#=> heroku-gem/2.29.0 (x86_64-linux) ruby/1.9.3

In order to fix it just open your terminal and run:

heroku pg:reset DATABASE --confirm YOUR_APP_NAME
heroku run rake db:setup
heroku restart
heroku open
1
votes

In my case, I also had to destroy and recreate my app. I had run a rake db:migrate with a migration file not committed and for whatever reason, the pg:reset wasn't working.

1
votes

Are you using devise? I had this exact issue when upgrading to 2.0 - You have to manually change the migration file.

https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

0
votes

Apparently deleting my entire app and then redeploying from scratch fixed it. I have no idea what the problem was.

0
votes

In my case, the symptoms were the same, but the root cause and remedy turned out somewhat different. Spent hours on this. Hopefully this post will save someone else those hours! I am using:

Everything runs fine locally on SQLite, but get the same PG error on Heroku. Turns out that ActiveScaffold somehow prevents Heroku push from successfully running rake tasks due to an error similar to above. So you get a cache 22 where you get the same error if you try to run heroku rake db:migrate or similar.

Now the fix:

  • Comment out code blocks similar to following from all controllers that use "active_scaffold":

    active_scaffold :<model_name> do |conf|
    end
    
  • Commit, push to heroku
  • heroku run rake db:migrate
  • Verify that everything is fine by running heroku run rails console and then say creating a model and saving it.
  • Now revert the changes (i.e. bring back the active_scaffold block above)
  • commit, push to heroku
  • you are in business!
0
votes

I keep my local setup as close to production as possible, including using a postgresql database, so I had this problem on my local machine. I can't delete my production database anyway. It turned out my issue was only in test, so I used: rake db:test:prepare to fix it.

0
votes
rake db:drop
rake db:create
rake db:migrate
0
votes

I had a similar issue and ran heroku run rake db:reset and heroku run rake db:migrate to fix the issue. I guess that I just hadn't run the proper migrations to fix the problem.

0
votes

There may be many reasons for this error. For my app, however, the issue was that I hadn't logged out of the app before I ran the migration (?). So going to this path: http://name_of_my_app.herokuapp.com/logout fixed the problem for me.

0
votes

After hours of sifting through answer, I realized that when you specify

rails new MYAPP -database POSTGRESQL

it changes the .gitignore file, ignoring the entire /db/ directory, so my database was never getting pushed up to heroku. Remove it with caution, or at least don't have your username and password in there where you push up.