0

I am building a small rails app. When, I run heroku run rake db:migrate, I get this error

rake aborted!

StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "categories" does not exist
: CREATE TABLE "habits" ("id" serial primary key, "name" character varying, "description" character varying, "category_id" integer, "user_id" integer, "created_at
" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_23642321ab"
FOREIGN KEY ("category_id")
  REFERENCES "categories" ("id")
, CONSTRAINT "fk_rails_541267aaf9"
FOREIGN KEY ("user_id")
  REFERENCES "users" ("id")
)

In attempt to solve it, I also added this to inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'category', 'categories'

  inflect.plural 'category', 'categories'
end

Which didn't help.

I also looked at few answers on stackoverflow including this, but it didn't help because I am getting this error when I run migration command.

Here is my migration files for categories and habits.

class CreateCategories < ActiveRecord::Migration[5.0]
  def change
    create_table :categories do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateHabits < ActiveRecord::Migration[5.0]
  def change
    create_table :habits do |t|
      t.string :name
      t.string :description

      t.integer :user_id
      t.integer :category_id

      t.timestamps
    end
  end
end

Here is my schema.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170612231416) do

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.text     "description"
    t.integer  "habit_id"
    t.integer  "user_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "goals", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "goals_habits", force: :cascade do |t|
    t.integer  "habit_id"
    t.integer  "goal_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "goals_milestones", force: :cascade do |t|
    t.integer  "goal_id"
    t.integer  "milestone_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  create_table "habits", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.integer  "user_id"
    t.integer  "category_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "milestones", force: :cascade do |t|
    t.string   "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "milestones_statuses", force: :cascade do |t|
    t.integer  "milestone_id"
    t.integer  "status_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  create_table "statuses", force: :cascade do |t|
    t.string   "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.integer  "role"
  end

end

Not sure what more I am missing!

Hima Chhag
  • 401
  • 7
  • 22

3 Answers3

1

It seems there is a problem with your migrations. Run this locally to see if they run without a problem: rake db:drop && rake db:create && rake db:migrate

PS: I don't tell you to run rake db:reset because that loads the schema instead of running the migrations.

Anna S
  • 157
  • 1
  • 9
Amin Arria
  • 46
  • 3
0

It kind of seems like your migrations files are the problem. Perhaps try rake db:schema:load instead. I have had similar problems before and it is always because of a column added after the initial migration.

ekr990011
  • 724
  • 2
  • 7
  • 23
  • Hmm could we see your schema? Also not sure but this might help https://stackoverflow.com/questions/5450930/heroku-postgres-error-pgerror-error-relation-organizations-does-not-exist?rq=1 – ekr990011 Jun 13 '17 at 18:13
  • Thanks a lot for your time. I have edited my question to include schema.rb – Hima Chhag Jun 13 '17 at 19:00
  • Alright my best guess is still which order the migrations are being done. The checked answer on this one might be one solution: https://stackoverflow.com/questions/30290250/pgundefinedtable-error-relation-does-not-exist Other wise I hate to say it but a heroku run rake db:reset or rake db:drop:all and then re-trying the migrations. These options are off the table if you have data that you don't want to destroy however so warning. – ekr990011 Jun 13 '17 at 19:37
  • Also depending on how you did the reference in your models the migration might have to be a t.references setup. http://edgeguides.rubyonrails.org/active_record_migrations.html ctrl+f references the second one: add_reference :products, :user, foreign_key: true Good luck I hope something works for you! – ekr990011 Jun 13 '17 at 19:42
0

Finally, I just had to destroy my Heroku app and recreate it. That solved this problem. I didn't have much data in my app. So, I could do it. If you have a really good database, I wouldn't suggest it.

To destroy app, heroku apps:destroy

And to create it again, heroku create appname

Hima Chhag
  • 401
  • 7
  • 22