我经常使用Factory Girl在我的数据库中创建虚假数据。但是每当我创建一个新工厂并且发生错误时,我最终都会遇到大量不一致的数据,因为它不支持事务,不会发生回滚 例如(控制台快照)
INSERT INTO `INVOICES` (`created_by`, `creation_date`, `fulfillment_branch_id`, `invoice_date`, `invoice_number`, `invoice_status`, `last_updated`, `last_updated_by`, `order_id`, `order_status`, `parent_order_id`) VALUES ('xx.xx@xx.net', '2015-07-16 11:27:00', '1035280', NULL, NULL, 'Active', '2015-07-16 11:27:00', 'xx.xx@xx.net', 24, 'Invoiced', NULL)
SQL (0.8ms) INSERT INTO `packages` (`created_at`, `created_by`, `shipment_id`, `size`, `status`, `updated_at`, `updated_by`, `value`, `weight`) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545)
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'shipment_id' cannot be null: INSERT INTO `packages` (`created_at`, `created_by`, `shipment_id`, `size`, `status`, `updated_at`, `updated_by`, `value`, `weight`) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545)
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/db-charmer-1.9.0/lib/db_charmer/rails3/abstract_adapter/connection_name.rb:14:in `instrument'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:234:in `exec_insert'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/relation.rb:66:in `insert'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/persistence.rb:367:in `create'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/timestamp.rb:58:in `create'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in `block in create'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in `_run__221280857__create__997484455__callbacks'
... 58 levels...
from /home/infibeam/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/observer.rb:194:in `notify_observers'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/evaluation.rb:20:in `notify'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:13:in `block in result'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:in `tap'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:in `result'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory.rb:42:in `run'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications.rb:125:in `instrument'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:22:in `run'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
from (irb):4
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from scripFactoryGirl.reload
任何人都可以解释我应该在哪里修补补丁以确保工厂以交易方式运行吗?
要清除我的问题,我想修补FactoryGirl模块,以便每当我调用它时,我的FactoryGirl.create api都会被包装在ActiveRecord事务中。
谢谢
答案 0 :(得分:1)
您为包表设置此错误的原因
shipment_id: nil
要解决此问题,您应该为此设置一些信息。例如:
FactoryGirl.define do
factory :example do
shipment
#your another code
装运地点 - 是装运模型的关联