【问题标题】:How to use FactoryGirl with ActiveRecord transaction rails如何将 FactoryGirl 与 ActiveRecord 事务轨道一起使用
【发布时间】:2015-07-16 06:37:59
【问题描述】:

我经常使用 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 事务中。

谢谢

【问题讨论】:

  • 您的意思是在运行时它会在数据库中创建记录,如果失败则将记录保留在数据库中?如果是这种情况,请使用数据库清理器来避免这种情况
  • 您是否有理由不能将数据创建包装在事务中?通常,FactoryGirl 将在测试环境中使用,如果它失败并不重要,因为无论如何都会清理数据库。或者,如果您在种子中使用它,您可以将调用包装在事务中。如果您通过控制台创建数据,那么在种子文件中执行它可能更可重复。
  • 我有时会在开发中使用 FactoryGirl 来创建假数据,但在此过程中大部分时间我以大量不一致的数据结束

标签: mysql ruby-on-rails ruby-on-rails-3 activerecord factory-bot


【解决方案1】:

这个错误的原因是你为包表设置的

shipment_id: nil 

要解决这个问题,您应该为此设置一些信息。示例:

FactoryGirl.define do
  factory :example do
    shipment

    #your another code

shipment 在哪里 - 是 Shipment-model 的关联

【讨论】:

  • 错误只是给出问题的一个例子。问题不是寻找错误本身的修复方法,而是寻找错误导致的连锁反应的解决方案。
  • 谢谢...这只是一个示例,但我需要更多通用解决方案来说明如何围绕 ActiveRecord 事务包装我的 FactoryGirl 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
相关资源
最近更新 更多