thoughtbot / factory_bot_rails

Factory Bot ♥ Rails
https://thoughtbot.com/services/ruby-on-rails
MIT License
3.07k stars 369 forks source link

error creating database from db/schema.rb #97

Closed abacha closed 11 years ago

abacha commented 11 years ago

I'm creating my test db using RAILS_ENV=test bundle exec rake db:drop db:create db:schema:load --trace and its raising a strange error.

I can build the database If I move my factories to another folder and then move back the factories into the project

gems
    factory_girl (4.1.0)                                                         
       activesupport (>= 3.0.0)                                                   
    factory_girl_rails (4.1.0)                                                   
        factory_girl (~> 4.1.0)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:drop
** Invoke db:create (first_time)
** Invoke db:load_config 
** Invoke rails_env 
** Execute db:create
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Invoke rails_admin:disable_initializer (first_time)
** Execute rails_admin:disable_initializer
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
** Execute environment
rake aborted!
Mysql2::Error: Table 'bigbang_test.clients' doesn't exist: SHOW FULL FIELDS FROM `clients`
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:259:in `execute_and_free'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:426:in `columns'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `yield'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `default'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:228:in `columns'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:237:in `columns_hash'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:19:in `descends_from_active_record?'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/inheritance.rb:25:in `finder_needs_type_condition?'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/base.rb:455:in `relation'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/scoping/named.rb:37:in `scoped'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activerecord-3.2.11/lib/active_record/querying.rb:9:in `includes'
/home/duke/projects/simplific/bigbang/app/models/person.rb:3:in `<class:Person>'
/home/duke/projects/simplific/bigbang/app/models/person.rb:1:in `<top (required)>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:359:in `require_or_load'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:502:in `load_missing_constant'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:192:in `block in const_missing'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:190:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:190:in `const_missing'
/home/duke/projects/simplific/bigbang/spec/factories/people_factories.rb:12:in `block in <top (required)>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
/home/duke/projects/simplific/bigbang/spec/factories/people_factories.rb:3:in `<top (required)>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:19:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `call'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/duke/projects/simplific/bigbang/config/environment.rb:5:in `<top (required)>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/application.rb:103:in `require_environment!'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/railties-3.2.11/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/home/duke/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:187:in `block in invoke_prerequisites'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:185:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:185:in `invoke_prerequisites'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:165:in `block in invoke_with_call_chain'
/home/duke/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/bin/rake:23:in `load'
/home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/bin/rake:23:in `<main>'
Tasks: TOP => db:schema:load => environment
joshuaclayton commented 11 years ago

I'm guessing what's happening is when you drop the database, and FactoryGirl is present, it's going to look for the tables (because it's interacting with AR) and AR is raising, yelling that the table isn't found. I think the more appropriate rake task for this is rake db:test:prepare, which should clear out the test database and set up the DB based on the schema. Does running that one rake task work?

abacha commented 11 years ago

it still raises the same error if I only ran db:create without db:drop and also with db:test:prepare

abacha commented 11 years ago

I've commented all People factories and it worked. aybe it has something to do about Person being a STI?

joshuaclayton commented 11 years ago

It looks like STI is working fine. So, to clarify, instead of having to set RAILS_ENV or run db:create db:migrate you should be able to run just rake db:create db:migrate db:test:prepare and everything should work. If it's still breaking, do you have any migrations that refer to any of your AR classes at all?

abacha commented 11 years ago

I'm just running the exact same command as Circle CI, I don't usually build the db like that.

Yes, I have some migrations referring to AR classes, but why it would be a problem only with factories?

joshuaclayton commented 11 years ago

I'm actually not sure, to be honest. Can you create a demo app that demonstrates this issue (or give me read access) and point me to the repo on GitHub? Debugging things like via stacktraces without being able to interact with the app is very difficult to do.

abacha commented 11 years ago

I'm doing some tests here but I cannot isolate the problem. I have an STI (Person,Company,Fund) < Client, and the exception is raising on my Person Factories

 factory :person do
    document { Faker::CPF.pretty }
    sequence(:name) { |n| "JOHN DOE #{n}" }
  end

I can build the db if I do

 factory :person, class: Client do
    document { Faker::CPF.pretty }
    sequence(:name) { |n| "JOHN DOE #{n}" }
  end

but there are other Client Factories (Fund and Company) and they work without referencing the Client class

abacha commented 11 years ago

I think I've discovered the issue. the factory that I've mentioned before wasn't the problem, the issue was here:

factory :person_1, class: Person do
    name "JOSÉ DA SILVA"
    document "272.166.011-06"
    phone_mobile_code "87"
    phone_mobile_number "88999988"
    email "JOSE@GMAIL.COM"
    association :info, factory: :person_info_1
  end

when Person was called, the table missing exception was raised, I've fixed it by replacing Person with "Person"

thanks for your time!

joshuaclayton commented 11 years ago

@abacha whew, glad to hear you got it figured out!

fluxsaas commented 9 years ago

i think you can solve this also by changing the Gemfile to:

gem 'factory_girl_rails', :require => false

instead of:

gem 'factory_girl_rails'

and then in your spec_helper.rb:

require 'factory_girl_rails'

see: http://stackoverflow.com/a/12425729/571111

DemitryT commented 9 years ago

@fluxsaas Yes, that solved the issue for me, muchas gracias :+1: I have tried many solutions, including the one above and yours was the only that worked. The stack trace from CircleCI wasn't super helpful.