mysql - 如何将 FactoryGirl 与 ActiveRecord 事务轨结合使用

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

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

谢谢

最佳答案

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

shipment_id: nil 

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

FactoryGirl.define do
  factory :example do
    shipment

    #your another code

发货在哪里 - 是发货模型的关联

关于mysql - 如何将 FactoryGirl 与 ActiveRecord 事务轨结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31446898/

相关文章:

php - .htaccess 页面 url 出现问题

选择查询的 PHP mySQL 错误

ruby-on-rails - 通过使用 Rails 将 collection_select 与 has_many 一起保存

ruby-on-rails - 向 f.submit 添加一个类但保留默认功能?

ruby-on-rails - Rails 中的大小写不敏感

python - Django,选择查询集覆盖值

php - 初学者PHP/mySQL问题

ruby-on-rails - 生成 Rails 迁移,如何将迁移文件名自动放入剪贴板?

ruby-on-rails - youtube使用什么框架?

ruby-on-rails - 使用 Puma 时初始化 BigDecimal 限制的正确位置在哪里