thoughtbot / factory_bot

A library for setting up Ruby objects as test data.

Home Page:https://thoughtbot.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

owst opened this issue · comments

This feature request came from rubocop/rubocop-factory_bot#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?