Search code examples
ruby-on-rails-4heroku

Heroku Rails - ActiveRecord::UnknownAttributeError (unknown attribute 'user_id' for Timetable.):


This problem has me stumped.

When deployed to Heroku i got the error but not locally which would indicate a problem with the db

Line its referring to

 class StaticPagesController < ApplicationController

  def home

    if logged_in?
      @project = current_user.projects.build if logged_in?
      **@timetable = current_user.timetables.build if logged_in?**
      @feed_items = current_user.feed.paginate(page: params[:page]).reorder("project_due_date ASC")
      @feed_items3 = current_user.feed3.paginate(page: params[:page], :per_page => 1)
    end
  end

I've tried

Heroku run db:migrate and Heroku restart 

Still same error

It may be the fact that i have the schema wrong somehow but when woud that affect Heroku but not localhost?

URL = https://radiant-sea-5676.herokuapp.com/

Edit - It seems to work when not logged in on Heroku but crashes when logged in. Works either way on localhost.

Edit Again - Checking the schema through Heroku shows there is no User iD column and no Index setup for the User iD even after a migrate.

    create_table "timetables", force: :cascade do |t|
    t.string   "name",       limit: 255
    t.string   "attachment", limit: 255
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

Solution

  • Step 1: Reset your database locally:

     rails db:drop:db:create db:migrate db:schema:dump db:setup
    

    If you encounter an error for db:drop you can use the :_unsafe way to do it! I'm not sure what this will do but it will force the database to be dropped! (YOU WILL LOOSE ALL LOCALLY STORED DATA)

    So, if you encountered that error use this (not step 1):

     rails db:drop:_unsafe db:create db:migrate db:schema:dump db:setup
    

    STEP 2: git and GitHub :

    git add .
    git commit -am 'dropped and recreated DB'
    git push
    

    STEP 2: Reset your Heroku database:

    Greate discussions in this link!

    heroku restart
    heroku pg:reset DATABASE            (no need to change the DATABASE)
    heroku run rake db:migrate
    heroku run rake db:seed             (if you have seed)