10

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

Community
  • 1
  • 1
Richard Burton
  • 2,230
  • 6
  • 34
  • 49

7 Answers7

15
create_table :posts

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

Eran
  • 387,369
  • 54
  • 702
  • 768
Robin
  • 21,667
  • 10
  • 62
  • 85
12

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

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
ajbraus
  • 2,909
  • 3
  • 31
  • 45
4

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
mobilemonkey
  • 180
  • 1
  • 4
  • 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. – mu is too short Feb 10 '12 at 18:42
  • 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. – mobilemonkey Feb 10 '12 at 19:03
  • 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. – mu is too short Feb 10 '12 at 19:18
3

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.

Arcolye
  • 6,968
  • 4
  • 34
  • 28
1

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.

Webjedi
  • 4,677
  • 7
  • 42
  • 59
0

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.

deefour
  • 34,974
  • 7
  • 97
  • 90
-1

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

mtminogue
  • 31
  • 1
  • 5