ChrisM
ChrisM

Reputation: 716

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

Upvotes: 3

Views: 2757

Answers (5)

Loizos Vasileiou
Loizos Vasileiou

Reputation: 704

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)

Upvotes: 2

Jackie Johnston
Jackie Johnston

Reputation: 231

You shouldn't need to drop or reset your database. Apparently when you are running in production mode the database schema is cached. So whenever you do a migration, you need to follow it up with a heroku restart command for the updated schema to load.

Upvotes: 14

Nuclearman
Nuclearman

Reputation: 5304

Found a good solution for when the schemas are out of sync, but it requires destroying the database and building up a clean one. Resetting the database keeps the schema the way it is and thus isn't useful.

  1. Run rake db:drop db:create db:migrate db:schema:dump db:setup locally and push any schema changes to to heroku.
  2. Restart your dynos via heroku restart this will cut all database connections so you don't run into issues. You can also switch to maintenance mode.
  3. Run heroku run 'rake db:drop db:create db:migrate db:schema:dump db:setup'.

It's a bit hacky, but basically performs a hard reset on the database. You may not need to run the last two commands.

Upvotes: 1

ChrisM
ChrisM

Reputation: 716

Until someone comes with a better explanation or answer what i had to do was go to the Heroku dashboard and delete the ClearDB database, then create a new one.

I configured the app with

heroku config | grep CLEARDB_DATABASE_URL

heroku config:set DATABASE_URL='mysql://adffdadf2341:[email protected]/heroku_db?reconnect=true'

Then ran heroku run rake db:migrate and it rebuilt the database with the correct Schema including foreign keys.

Upvotes: 2

stephenmurdoch
stephenmurdoch

Reputation: 34613

Ignore my last answer, I am seeing the issue when I login to your site and visit the timetable page.

It's possible that your schema is corrupt, this can happen from time to time, especially if you alter migrations after running them.

Try the following:

  • on localhost, delete your schema, drop your database and migrate again
  • on localhost, delete all cookies from your browser or try using another one as it's possible an old cookie is masking the problem
  • on heroku, after pushing your new schema, run heroku pg:reset DATABASE --confirm name-of-your-app and than rake db:migrate again

Hope this helps

It's important to find out why this happened though, so can you advise as to whether or not you have modified any of your migrations after running them? I learned the hard way that you can't do that!

Upvotes: 1

Related Questions