45

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Mark
  • 823
  • 2
  • 10
  • 16
  • 1
    The organizations table doesn't seem to exist. did you run your migration? – Shreyas Mar 27 '11 at 17:33
  • 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 Mar 27 '11 at 17:39
  • Also, when I log into the console and check: >> ActiveRecord::Base.connection.tables => ["schema_migrations", "jobs", "organizations", etc... – Mark Mar 27 '11 at 17:55
  • Now that you have your organizations table created, are you still facing a problem? – Shreyas Mar 27 '11 at 18:00
  • 1
    Yes, the organizations table was already created when the problem occurred, thanks... – Mark Mar 27 '11 at 18:03

13 Answers13

83

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

Orlando
  • 9,374
  • 3
  • 56
  • 53
akipponn
  • 846
  • 7
  • 5
21

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

heroku rake db:migrate

:)

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Victor S
  • 5,098
  • 5
  • 44
  • 62
19

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

a.b.d
  • 2,190
  • 3
  • 26
  • 26
  • 1
    I fought with Heroku for a good forty minutes before I thought to try this. Redeploying, dropping(reset) and migrating the database solved nothing. – Chris Cummings Feb 06 '12 at 05:01
7

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
Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
1

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

Tim Fletcher
  • 7,062
  • 1
  • 35
  • 33
1

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.

0

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!
paneer_tikka
  • 6,232
  • 1
  • 20
  • 17
0

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
rake db:drop
rake db:create
rake db:migrate
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
0

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.

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
dominathan
  • 61
  • 1
  • 6
0

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.

gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
0

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.

Dave Sloan
  • 13
  • 3
0

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

Mark
  • 823
  • 2
  • 10
  • 16