1

I am deploying my rails app for the first time and I got the following error:

Migrating to Changetimezone (20140605033929) == 20140605033929 Changetimezone: migrating =================================== -- change_column(:opportunities, :updated_at, :date) PG::UndefinedTable: ERROR: relation "opportunities" does not exist : ALTER TABLE "opportunities" ALTER COLUMN "updated_at" TYPE date rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR: relation "opportunities" does not exist

Here is my opportunities model:

class Opportunity < ActiveRecord::Base
before_save :check_for_nil,:calculate_derated_value
has_many :updates, dependent: :destroy
has_many :activities, dependent: :destroy   
has_many :contacts, dependent: :destroy
has_many :links, dependent: :destroy

Clearly, it has associations. I'm not sure why the complaint is that it doesn't have associations... all the models that belong to it have a :belongs_to opportunities declaration

I'm at a complete loss of what might be going on. I've tried searching but I can't find any solutions that fix this exact problem.. does anyone have any ideas??

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • 4
    Have you run $ heroku run rake db:setup ? that one always gets me. – steel Jun 06 '14 at 03:49
  • PERFECT!!! @steel could you explain to me why that works and/or had to be done?? – Trung Tran Jun 06 '14 at 04:07
  • 2
    Heroku loads your migrations, but doesn't run them until you tell it to. However, your app has become dependent on the existence of those tables. So you run the above code (a combo of rake db:migrate followed by rake db:seed) and Heroku then has the tables it needs. – steel Jun 06 '14 at 04:09

1 Answers1

3

Could you explain to me why that works and/or had to be done??

Databases

When you deploy Rails into a production environment (local is development), it's recommended you set up another database to handle the production input from users.

The problem is running rake db:migrate does not work on both the local & production database simultaneously; only works on the environment the command is called.

This means if you're pushing your code to Heroku, you need to ensure you've ran all the migrations for it; as without them, you won't have the required changes.

--

As per the comments, it seems calling rake db:setup worked for you too. I'm not sure why this would work over the migrations (as rake db:setup just sets up the dbs as per the schema file):

Difference between rake db:migrate db:reset and db:schema:load

  • db:migrate runs (single) migrations that have not run yet.
  • db:create creates the database
  • db:drop deletes the database
  • db:schema:load creates tables and columns within the (existing) database following schema.rb
  • db:setup does db:create, db:schema:load, db:seed
  • db:reset does db:drop, db:setup
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147