rake db:migrate produces "rake aborted! could not find table" error
Asked Answered
H

4

8

I'm new to Rails and I'm building a simple project-tracking app for my employer. I've been developing the app on my Mac and pushing it to github. I just managed to clone my github repo to a windows box behind my company's firewall in hopes of letting colleagues try the app out.

But when I go to rake db:migrate to initialize the database on the windows box, I get the following error messages:

$ 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

My gems list looks like this:

$ 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)

My schema.rb file on the Windows box looks like this:

# 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

Since this is the first time I've run migrations on this machine, I would expect that nothing about the migration process should seek to access the 'projects' table before it's created. But I notice that the "projects" table isn't listed in schema.rb until after several tables that are associated with it (assets, macroposts.) Is that the source of the problem?

Or is some sort of gem dependency issue at work here? I noticed that the 'faker' gem shows up towards the top of the error listing, even though I don't even really make use of it except in my tests (copied from Michael Hartl's RailsTutorial.org.)

Thanks for any help or suggestions you can offer!

Dean Richardson Genlighten.com

Haynie answered 21/9, 2010 at 15:3 Comment(0)
R
13

I had this exact problem for the last few weeks on a side project I've been working on. The answer only became clear to me when I ran rake db:migrate with --trace. Once there I noticed that the stack trace was complaining about factory_girl not being able to instantiate the missing table item.

I went into my factory.rb file and removed the offending model and after that I was able to run rake db:migrate. In addition, the problem for me was:

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

In this case the Factory inside the factory was attempting to insert during the rake task. Since the table didn't exist yet it blew up. With a little help I found that the correct way to do this is:

Factory.define :table1 do |b|
  b.name "table1"
  b.rating 5
  b.table2_relation {Factory(:table2)}
end
Reeding answered 17/6, 2011 at 12:52 Comment(1)
Any idea why factory girl should be doing anything like this during schema:load in the first place? Shouldn't schema:load just use database.yml and schema.rb?Zavras
S
5

I just encountered this same problem. rake db:create would work, but running anything else, like rake db:schema:load or rake db:migrate, failed with an error about a missing table. Chris Hein's answer pointed me in the right direction. The missing table was being referenced in the specs, and for some reason they were being loaded as part of the environment before the rake tasks were running. I just moved the specs folder, ran the rake tasks, and the moved it back.

Shepp answered 29/9, 2011 at 4:40 Comment(1)
That was exactly what happened to me. So following what you said I commented code in an initialiser file (excuse the British spelling) with code referencing that model, then rake db:schema:load (because just rake db:migrate was still running into trouble), and voila!, it worked. Thanks!Sterilize
R
0

might want to try to rake db:create also make sure your config files are correct in pointing to the sql server you want.

Also make sure all of your migration files look good and don't have any syntax errors or anything like that.

Retire answered 21/9, 2010 at 15:6 Comment(4)
The environment.rb and database.yml files both seem to point to sqlite3 and the appropriate sqlite3-ruby gem. It's possible that my sqlite3 files are somehow incompatible with my other Rails files, but if so, it's not obvious how. My migration files work on other machines, so I'm pretty confident of them.Haynie
I deleted the empty development database and ran rake db:create as you suggested. No "could not find table" error was raised this time. I then tried to start the server ("ruby script/server") and got another lengthy error dump which started with "could not find table 'projects'. So the basic problem is still there.Haynie
you have to run migrations after creating the database, so I would run rake db:migrate after running rake db:createRetire
In the end, I copied an existing development database from my Mac to the Windows server and it worked fine. Thanks for your advice!Haynie
P
0

Had similar issue and it was to do with factory_girl as Chris pointed out. This way seems like an easier way to solve it:

In Gemfile, define factory girl like this:

gem 'factory_girl_rails', :require => false

Then in spec_helper.rb file:

require 'factory_girl_rails'

Here is the link to original answer: FactoryGirl screws up rake db:migrate process

Parsifal answered 6/10, 2014 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.