4

I am trying to set up a Rails app in new workstation. But when I try to run the migrations it throws error.

**rake aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'test_rb.roles' doesn't exist: SHOW FULL FIELDS FROM `roles`**
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record        /connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
 /home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activesupport-3.2.21/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 /home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/mysql2_adapter.rb:213:in `execute'
/home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:259:in `execute_and_free'
 /home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:426:in `columns'
 /home/suganya/.rvm/gems/ruby-2.1.1@rb/gems/activerecord-3.2.21/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'

I don't know what am I doing wrong?

 /home/suganya/academics/reportbee/app/models/constant_cache.rb:4:in `caches_constants'
  /home/suganya/academics/reportbee/app/models/role.rb:14:in `<class:Role>'
 /home/suganya/academics/reportbee/app/models/role.rb:1:in `<top (required)>'

In Role.rb

 class Role < ActiveRecord::Base
     has_many :allotments
     has_many :users, :through => :allotments

      serialize :possible_display_names, Array

     validates :name, presence: true, uniqueness: true

   scope :accessible, -> { where( :accessible => true ) }

 CLASS_TEACHER_DISPLAY_NAME = 'Class Teacher'

   extend ConstantCache::ClassMethods
   caches_constants :name, :converter => :titleize 
end
Suganya
  • 701
  • 1
  • 9
  • 27
  • What's in the `db/migrate/` folder? Is this an app you created from scratch, or is it from someone else? Also, what about the database - did you create it from scratch with `rake db:create` or did you copy an existing database? Looks like when you run `rake db:migrate` the migration being run is expecting the `roles` table to be there. – Prakash Murthy Dec 28 '14 at 04:00
  • I cloned this app. I did not create it from scratch. Also I did rake db:create and then tried rake db:migrate. In this case it is throwing this error. – Suganya Dec 28 '14 at 04:03
  • Check the error message; it should say which migration is causing the error. How many files are in the `db/migrate` folder? – Prakash Murthy Dec 28 '14 at 04:24
  • If you are not able to identify which migration is causing the error, retry with 1. `rake db:drop` 2. `rake db:create` and 3. `rake db:migrate` ; might give more info about the error. – Prakash Murthy Dec 28 '14 at 04:25
  • I have nearly 30 migrations. I tried 1) rake db:drop 2) rake db:create 3) rake db:migrate, But still the same error persists. – Suganya Dec 28 '14 at 04:30
  • Does the `rake db:migrate` step run any of the migrations successfully? If it does, then you should be able to see which migration is failing. If it is not, then the oldest migration (file with the oldest time stamp in `db/migrate` folder is the culprit. Running `rake db:migrate:status` will show which migrations have been run and which are not. – Prakash Murthy Dec 28 '14 at 04:45
  • rake db:migrate:status also throw the same error :-( – Suganya Dec 28 '14 at 04:49

2 Answers2

9

Your migrations are seemingly in an inconsistent state, which is fine. Instead of rake db:create and rake db:migrate, you should simply be running rake db:setup to create the database and restore the actual authoritative state of the database from db/schema.rb.

You are not supposed to be able to clone any given repo and run rake db:migrate to go all the way through every version of the database that has ever existed. This is a common misconception with Rails. db/schema.rb contains the most recent state of the database, and that is the one and only authoritative source of database schema. You're supposed to just load that file.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 3
    This is good information, but I get the same error for db:setup as I do for db:migrate. – k-den Aug 31 '15 at 23:13
  • My problem was described here: http://stackoverflow.com/questions/12423273/factorygirl-screws-up-rake-dbmigrate-process – k-den Aug 31 '15 at 23:46
  • Another possible cause of this error message is differing column types, described here: https://stackoverflow.com/questions/44550062/migration-to-create-table-raises-mysql2error-table-doesnt-exist?rq=1 – Loren May 30 '18 at 15:21
3

Sometimes there are routes in your /config/routes.rb file that demand existence of DB tables (Devise and ActiveAdmin routes in my case). Just comment them out before running migrations and restore afterwards.