Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specific factories to skip primary key definition checks #444

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zorab47
Copy link

@zorab47 zorab47 commented Dec 14, 2023

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants