3

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

5 Answers 5

14

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

2

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.

Comments

1

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!

3 Comments

Localhost works fine. Actually enver had a problem with that but when i check Schema on Heroku using heroku run "bundle exec rake db:schema:dump && cat db/schema.rb" its missing add_index "timetables", ["user_id"], name: "index_timetables_on_user_id", using: :btree... Its there for projects. Where did this come from for projects? It looks its auto generated as theres no user_id param for projects but something is adding it
Pastebin of the Schema pastebin.com/1DJF69Zr. Notice how the Index line is missing after the Timetables and the very last line about the foreign key also.
Literally had to remove the ClearDB app from Heroku, setup a new one. change the "heroku config:set DATABASE_URL=" to the new ClearDB. After this the Heroku run rake:migrate processed properly and inserted the correct foreign keys. The local Command rake db:reset seems to be the solution but that command doesnt work on Heroky when using ClearDB.
1

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.

2 Comments

By pushing any schema changes you mean heroku run rails db:migrate?? Please update answer if that is the case!
No I mean running that command may affect the schema locally and you should push those changes to heroku, though then again, since we don't actually do a `db:schema:load`` so it doesn't really matter. Just follow the directions above and keep in mind it will wipe your heroku database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.