thoughtbot / factory_bot_rails

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

Allow specific factories to skip primary key definition checks #444

Open zorab47 opened 9 months ago

zorab47 commented 9 months ago

Note: This PR is only a proof of concept idea due to monkey-patching FactoryBot::Factory.

Problem

A new default in factory_bot_rails is to raise errors when a factory is defining a primary key. There are occasionally needs to assign a ActiveRecord model's primary key explicitly in a factory (see examples in #438). We don't want to entirely skip that additional check for every factory by disabling reject_primary_key_attributes for all factories.

Possible Solution

Add a per factory configuration to skip the check for specific factories.

FactoryBot.define do
  factory :warehouse, config: { allow_primary_key_definitions: true } do
    sequence(:id)
  end
end
mike-burns commented 8 months ago

Ah, I was thinking of passing through the config via the ActiveSupport::Notifications payload.