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

ID Generation > 4 byte limit causing ActiveModel::RangeError:

diabolo opened this issue · comments

Description

Often when creating objects Factory Bot uses ids that are too large causing errors like

ActiveModel::RangeError:
597483228327 is out of range for ActiveModel::Type::Integer with limit 4 bytes

Reproduction Steps

Generate a number of objects in a build

Expected behavior

ID's should default to 4 byte limit. If you want larger ids there should be a global config to allow them

Actual behavior

Sometimes you get to big an ID

System configuration

factory_bot version: 6.2
rails version: 6.1
ruby version: 2.7.4

Thanks for the issue.

FactoryBot.create doesn't do anything special with ids. It builds a new object, assigns attributes based on the factory definition, then calls save!. Rails takes care of the rest.

If you have spring running, keep in mind that the ids won't reset back to 1 each time you run the test suite, they will keep going up and up. If that is the case, you can stop spring to reset the ids back to 1.

Beyond that, I'd need a reproduction script to offer much more help. This is not a problem I've run into.

We are not running Spring. This happens occasionally. Our test suite probably creates thousands of objects. About once in every 5 builds one of these objects gets a big id and fails. A reproduction script isn't likely to work here. However there is some point in the FactoryBot code where an ID is assigned to a new object. If you could point me to that I could investigate, or you could just ensure that the ID generated is always limited to 4 bytes.

I've done some further investigation. I can see that factory_bot rarely assigns a number to an id, so I think this might be something to do with where the build is being run (which is on circleci). I certainly can't produce this error locally. Anyhow thanks for your time.