Search code examples
ruby-on-railssetup-projectdatabase-migration

can't run migrations. Table doesn't exists


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

Solution

  • 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.