factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

Raise an error if `sequence` is defined for attribute with auto-incrementing sequence in the DB

Open owst opened this issue 1 year ago • 0 comments

This feature request came from https://github.com/rubocop/rubocop-factory_bot/pull/52 which has an example reproduction of the problem.

Problem this feature will solve

E.g. with a simple factory:

  factory :post do
    sequence :id
  end

where the post table's id is backed by a DB sequence, it is quite easy for the FactoryBot sequence to get out of sync with the DB sequence and cause duplicate errors, for example with:

FactoryBot.create(:post)
Post.create!
FactoryBot.create(:post)

the second FactoryBot.create will error due to a duplicate id value in the DB.

Desired solution

Disallow defining a sequence for an attribute that is backed by an auto-incrementing sequence in the database.

Alternatives considered

Perhaps the RuboCop check is sufficient, and implementing the check/error in FactoryBot is not possible or deemed worth the effort?

owst avatar Jul 01 '23 12:07 owst