thoughtbot/factory_bot · error · FactoryBot::DuplicateDefinitionError
#{@component.name} already registered: #{name}
Error message
#{@component.name} already registered: #{name} What it means
DuplicateDefinitionError raised by Decorator::DisallowsDuplicatesRegistry#register when FactoryBot.define registers a factory, trait, or sequence whose name is already in the registry. The factories, traits, and sequences registries are wrapped with this decorator; the message's first word comes from @component.name and tells you which registry collided (Factory/Trait/Sequence).
Source
Thrown at lib/factory_bot/decorator/disallows_duplicates_registry.rb:6
module FactoryBot
class Decorator
class DisallowsDuplicatesRegistry < Decorator
def register(name, item)
if registered?(name)
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
else
@component.register(name, item)
end
end
end
end
end
View on GitHub (pinned to 18ae8b581b)
Solutions
- Find the collision: `grep -rn 'factory :user' spec/` (or trait/sequence names) and delete or rename one definition.
- If files are intentionally re-required (pry, reload tasks), call FactoryBot.reload between loads to clear registries first.
- Compose variations with traits instead of duplicating whole factories (`factory :user` + `trait :admin`).
- Guard conditional definitions: `unless FactoryBot.factories.registered?(:user)`.
Example fix
# before
# spec/factories/user.rb
FactoryBot.define { factory :user { name { 'Plain' } } }
# spec/factories/admin.rb (same run)
FactoryBot.define { factory :user { name { 'Admin' } } } # DuplicateDefinitionError
# after
# one file, variations via traits
FactoryBot.define do
factory :user do
name { 'Plain' }
trait :admin do
name { 'Admin' }
end
end
end Defensive patterns
Strategy: validation
Validate before calling
# guard against re-registration, e.g. in reloaded spec helpers
name = :user
if FactoryBot.factories.registered?(name)
warn "skipping re-definition of :user"
else
FactoryBot.define { factory(name) { name { 'Plain' } } }
end Type guard
->(name) { !FactoryBot.factories.registered?(name) } Try / catch
begin
FactoryBot.define { #...
}
rescue FactoryBot::DuplicateDefinitionError => e
# e.message names the registry and the duplicate; rename/remove one, or FactoryBot.reload
end Prevention
- Give factories unique names across files; model variations as traits instead of duplicate factories.
- Call FactoryBot.reload when re-requiring factory files (console, reload tasks).
- Keep all definitions in one canonical factories directory; avoid defining factories inside individual *_spec.rb files.
When it happens
Trigger: Two `factory :user` blocks loaded in the same run (same file, or two files under spec/factories); defining a global sequence with the same name twice; re-requiring a factories file from multiple spec helpers without FactoryBot.reload in between; `FactoryBot.define` executed again in a rake task or console session after definitions are already loaded.
Common situations: Copy-pasted factory files; spec support files required multiple times; reloading code in a rails console or with Spring where definitions persist; monorepos or shared engines where two apps define `factory :user`; renaming a factory while leaving the old definition behind.
Related errors
- Association '#{name}' received an invalid factory argument.
- Association '#{name}' received an invalid attribute override
- undefined method '#{name}' in '#{@definition.name}' factory
- Unexpected block passed to '#{name}' association in '#{@defi
- Attribute already defined: #{attribute.name}
AI-assisted analysis of thoughtbot/factory_bot@18ae8b581b (2026-08-21).
Data as JSON: /api/errors/595b723a98784084.
Report an issue: GitHub.