无法运行迁移。表不存在

时间:2014-12-28 03:47:04

标签: ruby-on-rails setup-project database-migration

我正在尝试在新工作站中设置Rails应用程序。但是当我尝试运行迁移时,它会抛出错误。

**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'

我不知道自己做错了什么?

 /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)>'

在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

2 个答案:

答案 0 :(得分:7)

您的迁移似乎处于不一致的状态,这很好。您应该只运行rake db:create来创建数据库,并从rake db:migrate恢复数据库的实际权威状态,而不是rake db:setupdb/schema.rb

您不应该能够克隆任何给定的repo并运行rake db:migrate来完成所有已存在的数据库版本。这是Rails的常见误解。 db/schema.rb包含数据库的最新状态, 是唯一的数据库模式的权威来源。您应该只加载该文件。

答案 1 :(得分:0)

有时候你的/config/routes.rb文件中有路由要求存在数据库表(在我的情况下是Devise和ActiveAdmin路由)。只需在运行迁移之前将其注释掉,然后再恢复。