benl96
benl96

Reputation: 284

Pushing to Heroku - ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist

I'm really frustrated right now.

I have my working rails application on local and want to deploy it to Heroku. So far so good.

Whenever I try to push my code to Heroku it spits the error that no tables were found. I did try to run heroku run rails db:migrate db:drop db:reset or whatever several times without any success at all.

development:
  <<: *default
  database: postgres
  encoding: utf8


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  adapter: postgresql
  database: fortest

production:
  <<: *default
  adapter: postgresql
  url: <%= ENV['DATABASE_URL'] %>

All of my tables and migrations are working fine on my local machine, problem is that I need to run those migrations on Heroku to create my database tables, but somehow can't because it fails when my precompile assets start.

    Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
remote:        LINE 8:                WHERE a.attrelid = '"users"'::regclass
remote:                                                  ^
remote:        :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
remote:                             pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
remote:                             c.collname, col_description(a.attrelid, a.attnum) AS comment
remote:                        FROM pg_attribute a
remote:                        LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
remote:                        LEFT JOIN pg_type t ON a.atttypid = t.oid
remote:                        LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
remote:                       WHERE a.attrelid = '"users"'::regclass
remote:                         AND a.attnum > 0 AND NOT a.attisdropped
remote:                       ORDER BY a.attnum

Edit:

In my heroku logs it says Build failed -- check your build logs and in my buid logs everything works until it comes to this path:

 Bundle completed (32.92s)
       Cleaning up the bundler cache.
-----> Installing node-v8.10.0-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
       LINE 8:                WHERE a.attrelid = '"users"'::regclass

Update:

Rake::Task["assets:precompile"].clear
namespace :assets do
  task 'precompile' do
    puts "Not pre-compiling assets..."
  end
end

With this snippet I was able to atleast push my files to heroku. However, I still have problems with all the db:migrate db:reset functions. db:migrate appears to have the same error.

rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 8:                WHERE a.attrelid = '"users"'::regclass (...)

db:create somehow throws me this error

FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.

I see all of the data (username, password, adapter, database, port, host) and they're all fine.

Upvotes: 2

Views: 1620

Answers (2)

jmcd
jmcd

Reputation: 4300

What's happening here is that you most likely have a devise_for :users in your routes.rb file, which is causing Rails to look for the Users table when it boots up, but as you haven't run your migrations yet the table can't be found.

Comment out the devise_for in your routes.rb and push to Heroku - the migrations will run, then uncomment the devise_for and push again.

In general it's better to have created the Users table first, then add devise to the model in another, later step.

Upvotes: 5

Denny Mueller
Denny Mueller

Reputation: 3615

How about

$ git push heroku $ heroku run rake db:migrate

and then another push to kick the asset precompile again.

Upvotes: 6

Related Questions