【问题标题】:rake db:migrate produces "rake aborted! could not find table" errorrake db:migrate 产生“rake aborted! could not find table”错误
【发布时间】:2011-04-15 06:24:40
【问题描述】:

我是 Rails 新手,我正在为我的雇主构建一个简单的项目跟踪应用程序。我一直在我的 Mac 上开发应用程序并将其推送到 github。我刚刚设法将我的 github 存储库克隆到我公司防火墙后面的一个 windows 盒子,希望让同事们试用这个应用程序。

但是当我去 rake db:migrate 初始化 windows 框上的数据库时,我收到以下错误消息:

$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
Could not find table 'projects'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite3_adapter.rb:29:in `table_structure'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/faker-0.3.1/lib/extensions/object.
rb:3:in `returning'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite3_adapter.rb:28:in `table_structure'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite_adapter.rb:228:in `columns'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1271:in `columns'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1279:in `columns_hash'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1578:in `find_one'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1569:in `find_from_ids'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:616:in `find'
c:/Rails_Projects/molex_app/config/routes.rb:15
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:226:in `draw'
c:/Rails_Projects/molex_app/config/routes.rb:1
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load_without_new_constant_marking'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:521:in `new_constants_in'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `load_routes!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `each'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `load_routes!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:266:in `reload!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:537
:in `initialize_routing'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:188
:in `process'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113
:in `send'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113
:in `run'
c:/Rails_Projects/molex_app/config/environment.rb:9
c:/RubyonRails/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `
gem_original_require'
c:/RubyonRails/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `
require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:156:in `require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:521:in `new_constants_in'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:156:in `require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks/misc.rake:4
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `cal
l'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `exe
cute'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `eac
h'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `exe
cute'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:578:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:588:in `inv
oke_prerequisites'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in `eac
h'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in `inv
oke_prerequisites'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:577:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:564:in `inv
oke'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2019:in `in
voke_task'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `ea
ch'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `st
andard_exception_handling'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1991:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1970:in `ru
n'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `st
andard_exception_handling'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1967:in `ru
n'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:31
c:/RubyonRails/Ruby187/bin/rake:19:in `load'
c:/RubyonRails/Ruby187/bin/rake:19

我的宝石列表如下所示:

$ gem list

*** LOCAL GEMS ***

actionmailer (2.3.5)
actionpack (2.3.5)
activerecord (2.3.5)
activeresource (2.3.5)
activesupport (2.3.5)
faker (0.3.1)
rack (1.0.1, 1.0.0)
rails (2.3.5)
rake (0.8.3)
sqlite3-ruby (1.3.0 x86-mingw32)
will_paginate (2.3.12)

我在 Windows 机器上的 schema.rb 文件如下所示:

# This file is auto-generated from the current state of the database. Instead of editing this file, 
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100915193510) do

  create_table "assets", :force => true do |t|
    t.integer  "project_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "macroposts", :force => true do |t|
    t.text     "content"
    t.integer  "user_id"
    t.integer  "project_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "projects", :force => true do |t|
    t.string   "title"
    t.integer  "status",             :limit => 255
    t.integer  "program_manager_id"
    t.integer  "design_engineer_id"
    t.string   "sales_engineer"
    t.string   "customer"
    t.string   "market_size"
    t.string   "project_code"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "projects", ["design_engineer_id"], :name => "index_projects_on_design_engineer_id"
  add_index "projects", ["program_manager_id"], :name => "index_projects_on_program_manager_id"

  create_table "statuses", :force => true do |t|
    t.string   "status_name"
    t.integer  "status_code"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "encrypted_password"
    t.string   "salt"
    t.string   "remember_token"
    t.boolean  "admin",               :default => false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["remember_token"], :name => "index_users_on_remember_token"

end

由于这是我第一次在这台机器上运行迁移,我希望迁移过程在创建“项目”表之前不会寻求访问它。但是我注意到“项目”表没有在 schema.rb 中列出,直到与其关联的几个表(资产、宏帖子)之后。这是问题的根源吗?

或者是某种宝石依赖性问题在这里起作用?我注意到'faker' gem 显示在错误列表的顶部,尽管我什至没有真正使用它,除非在我的测试中使用它(复制自 Michael Hartl 的 RailsTutorial.org。)

感谢您提供的任何帮助或建议!

迪恩·理查森 Genlighten.com

【问题讨论】:

    标签: ruby-on-rails rake migrate sqlite3-ruby


    【解决方案1】:

    在过去的几周里,我一直在从事的一个副项目中遇到了这个确切的问题。当我运行rake db:migrate--trace. 时,我的答案才变得清晰。我注意到堆栈跟踪抱怨 factory_girl 无法实例化丢失的表项。

    我进入我的 factory.rb 文件并删除了有问题的模型,之后我就可以运行 rake db:migrate。另外,我的问题是:

    Factory.define :table1 do |b|
      b.name "table1"
      b.rating 5
      b.table2_relation Factory(:table2)
    end
    

    在这种情况下,工厂内的工厂试图在 rake 任务期间插入。由于该表还不存在,所以它炸毁了。在一点帮助下,我发现正确的方法是:

    Factory.define :table1 do |b|
      b.name "table1"
      b.rating 5
      b.table2_relation {Factory(:table2)}
    end
    

    【讨论】:

    • 谢谢。这也是我的问题。
    • 知道为什么工厂女孩首先应该在 schema:load 期间做这样的事情吗? schema:load 不应该只使用 database.yml 和 schema.rb 吗?
    【解决方案2】:

    我刚刚遇到了同样的问题。 rake db:create 可以工作,但运行其他任何东西,如 rake db:schema:loadrake db:migrate,都会失败,并出现有关丢失表的错误。 Chris Hein 的回答为我指明了正确的方向。规范中引用了丢失的表,并且由于某种原因,它们在 rake 任务运行之前作为环境的一部分被加载。我只是移动了 specs 文件夹,运行了 rake 任务,然后将其移回。

    【讨论】:

    • 这正是发生在我身上的事。因此,按照您所说的,我在初始化文件(请原谅英国拼写)中使用引用该模型的代码注释了代码,然后是 rake db:schema:load(因为只是 rake db:migrate 仍然遇到麻烦),瞧!,它起作用了。谢谢!
    【解决方案3】:

    可能想尝试rake db:create 并确保您的配置文件正确指向您想要的 sql 服务器。

    还要确保所有迁移文件看起来都不错,并且没有任何语法错误或类似的东西。

    【讨论】:

    • environment.rb 和 database.yml 文件似乎都指向 sqlite3 和相应的 sqlite3-ruby gem。有可能我的 sqlite3 文件与我的其他 Rails 文件不兼容,但如果是这样,则不清楚。我的迁移文件可以在其他机器上运行,所以我对它们很有信心。
    • 我删除了空的开发数据库并按照您的建议运行 rake db:create。这次没有引发“找不到表”错误。然后我尝试启动服务器(“ruby 脚本/服务器”)并得到另一个冗长的错误转储,以“找不到表'项目'开始。所以基本问题仍然存在。
    • 你必须在创建数据库后运行迁移,所以我会在运行rake db:create后运行rake db:migrate
    • 最后,我将一个现有的开发数据库从我的 Mac 复制到了 Windows 服务器,它运行良好。谢谢你的建议!
    【解决方案4】:

    有类似的问题,正如 Chris 指出的那样,它与 factory_girl 有关。这种方式似乎是一种更简单的解决方法:

    在 Gemfile 中,像这样定义工厂女孩:

    gem 'factory_girl_rails', :require => false
    

    然后在spec_helper.rb文件中:

    require 'factory_girl_rails'
    

    这里是原始答案的链接:FactoryGirl screws up rake db:migrate process

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2014-10-22
      相关资源
      最近更新 更多