PGError: ERROR: relation "table_name" does not exist

22,139

Solution 1

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

Solution 2

create_table :posts

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
22,139
Richard Burton
Author by

Richard Burton

Working on Balance.io

Updated on July 09, 2022

Comments

  • Richard Burton
    Richard Burton almost 2 years

    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

  • mu is too short
    mu is too short about 12 years
    I'd guess that something related to the polymorphic association was trying to load the model class before the table behind it was created. The SQL in the question is what AR uses to figure out the table's schema. ActiveAdmin is sometimes a culprit in cases like this.
  • mobilemonkey
    mobilemonkey about 12 years
    Interesting, I am using ActiveAdmin. Up to this point I wasn't aware that ActiveAdmin could have played a part in this, especially in the db:migration.
  • mu is too short
    mu is too short about 12 years
    I'm pretty sure ActiveAdmin tries to preload some things in an application initializer and that initialization happens before db:migrate. I don't know parts of ActiveAdmin or the model associations triggers the problem but people have solved similar problems by temporarily disabling ActiveAdmin.
  • Uri Klar
    Uri Klar over 9 years
    Thanks, this was it for me
  • huertanix
    huertanix about 8 years
    This didn't work for me. Also, what parts of the model file would you comment out?