thoughtbot / thoughtbot/factory_bot

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

Open
#1,585 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the three-step reproduction in the issue: create a factory record, create a direct record, then create another factory record and observe the duplicate-ID failure. Done means defining a FactoryBot sequence for a database-backed auto-incrementing attribute is rejected with an error; the issue does not name files or tests to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.