Richard Burton
Richard Burton

Reputation: 2240

PGError: ERROR: relation "table_name" does not exist

I am trying to push a simple app up to heroku and run:

heroku rake db:migrate

But I get the following error:

rake aborted!
PGError: ERROR:  relation "posts" does not exist
:             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 = '"posts"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

My migration looks like this:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :source
      t.string :tweetid
      t.string :pure
      t.string :media
      t.string :destination
      t.datetime :time
      t.timestamps
    end
  end
end

And, after referring to another SO answer, I have included the following in my Gemfile:

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'pg'
end

Thank you in advance for any help!

--- UPDATE ---

The main reason I am confused is that this all works locally, just not when I run the migration on heroku.

Here is the error I get now:

rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.)

I have been looking at this question:

Heroku error when launch rails3.1 app missing postgres gem

I am almost-certain my database.yml should not look like this (seeing as I need to be running postgresql!!!):

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Serious apologies for the nubishness here. Thank you for your help in advance!

Also tried this link: Uploading to Heroku DB rake:migrate problem

Upvotes: 10

Views: 23672

Answers (7)

ajbraus
ajbraus

Reputation: 2999

I just ran: bundle exec rake db:migrate and it worked

Upvotes: 12

mtminogue
mtminogue

Reputation: 31

I was having the same problem. I ran heroku run rake db:migrate and that solved the problem.

Upvotes: -1

Robin
Robin

Reputation: 21884

create_table :posts

Didn't you forget the s? The table names should be plural.

Upvotes: 15

Arcolye
Arcolye

Reputation: 6958

If you're using ActiveAdmin, whichever table PG says doesn't exist, comment out the contents of that ActiveAdmin rb file.

For example, for this case PGError: ERROR: relation "posts" does not exist, comment out the entire contents of app/admin/posts.rb, then uncomment after you've done your migrations.

Upvotes: 3

deefour
deefour

Reputation: 35360

In my case, I was doing rename_table in my migration, after having already modified my model name to reflect the new table name. I had moved User into User::User. The users table needed to be renamed to user_users, so my migration looked like

class RenameUsersTableWithPrefix < ActiveRecord::Migration
  def up
    rename_table :users, :user_users
  end

  def down
    rename_table :user_users, :users
  end
end

Instead of

app/models/user.rb

I now had

app/models/user.rb
app/models/user/user.rb

with the latter containing the User::User model, and the former containing simply

module User
  def self.table_name_prefix
    "user_"
  end
end

It was this class method in the newly added User module that was giving me the PGError when running rake db:migrate like the OP had. Temporarily removing this class method while I ran my migration allowed the migration to run smoothly.

Upvotes: 0

mobilemonkey
mobilemonkey

Reputation: 180

I had a similar problem, but it wasn't caused by the migration or the Gemfile. I had 4 models setup in a polymorphic relationship. Removing the statement

belongs_to :assetable, :polymorphic => true, :dependent => :destroy

and removing the subclass' acts_as_* declarations was enough to enable the db:migrate to successfully complete. I then added back the statements in the models and everything worked great. I'm not exactly sure why this is the case, but if you are in a similar situation this may help temporarily until a better solution presents itself.

My situation is polymorphic multi-table inheritance scheme between one parent and 3 objects using http://mediumexposure.com/multiple-table-inheritance-active-record/ as a baseline.

Upvotes: 4

Webjedi
Webjedi

Reputation: 4737

Robin probably has it right but just in case...

Check the filename/timestamps on your migrations. These get run in sequence. I had an issue where a generator that made my migrations was putting the foreign table first...I switched the file names and it worked.

It's a long shot but I thought I'd put that out there.

Upvotes: 1

Related Questions