fhaider
fhaider

Reputation: 165

PG::UndefinedTable: ERROR: table "table-name" does not exist

I was messing with my database, creating and deleting tables. I deleted few migration files after pushing them to heroku. Previously I created a table 'moms'. Later I wanted to rename it, so I deleted 'moms' and created new table 'minutes_of_meetings'. I did $rake db:migrate and everything was done successfully and my app is running perfectly on localhost.

After pushing it to heroku, when I did $heroku run rake db:migrate, it generated the following log:

  ActiveRecord::SchemaMigration Load (0.7ms)  SELECT "schema_migrations".* FROM "schema_migrations"
  Migrating to DropMoms (20150823142852)
  (0.6ms)  BEGIN
             == 20150823142852 DropMoms: migrating =========================================
                 -- drop_table(:moms)
             (0.9ms)  DROP TABLE "moms"
             PG::UndefinedTable: ERROR:  table "moms" does not exist
             : DROP TABLE "moms"
             (0.5ms)  ROLLBACK
             rake aborted!
             StandardError: An error has occurred, this and all later migrations canceled:

                                                                                     PG::UndefinedTable: ERROR:  table "moms" does not exist
             : DROP TABLE "moms"/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
      /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'

I created a new table 'moms' in heroku so that it can be deleted when migration runs. I did this:

$ heroku run Ruby console for rails-app-name >> ActiveRecord::Migration.create_table :moms 

I also created a migration to create table 'moms'. But still the error persists.

EDIT:

This is my CreateMoms migration file:

class CreateMoms < ActiveRecord::Migration
  def change
    create_table :moms do |t|
      t.string :name
      t.timestamp null: false
    end
  end
end

When I run heroku run rake db:migrate:up

Running `rake db:migrate:up` attached to terminal... up, run.1729
rake aborted!
VERSION is required
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:78:in `block (3 levels) in <top (required)>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)
WARNING: Toolbelt v3.41.3 update available.

On heroku run rake db:migrate:down

Running `rake db:migrate:down` attached to terminal... up, run.6389
rake aborted!
VERSION is required - To go down one migration, run db:rollback
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <top (required)>'
Tasks: TOP => db:migrate:down
(See full trace by running task with --trace)
WARNING: Toolbelt v3.41.3 update available.

Upvotes: 3

Views: 2073

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

Caution

rake db:schema:load will wipe away all of your data from the heroku database. Please make sure you have your data backup on heroku. If you already don't have backup of your heroku database, you can easily do so by using Heroku PGBackups

Looks like your schema is messed up. Just load the schema to the database using rake db:schema:load and then run the migration again:

heroku run rake db:schema:load
heroku run rake db:migrate

Upvotes: 3

Related Questions