thoughtbot / thoughtbot/factory_bot
Precision of DateTime fields, when specified, do not match the DB schema
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When an object is created by FactoryBot, and a value with a greater precision than permitted by the DB schema is specified for one of its DateTime fields, the value for that field does not reflect the precision specified by the DB schema until the record is reloaded.
Reproduction Steps
Specs to reproduce:
describe 'FactoryBot' do
context 'when a DateTime field is specified with greater precision than allowed by the DB' do
let(:nanosecond_precision_datetime) { DateTime.parse("2025-01-01T11:11:11.123456789") }
let(:microsecond_precision_datetime) { DateTime.parse("2025-01-01T11:11:11.123456") }
let(:identity) { create(:identity, updated_at: nanosecond_precision_datetime) }
it 'does not truncate the DateTime to the DB precision until the record is reloaded' do
expect(identity.updated_at).to eq(nanosecond_precision_datetime)
identity.reload
expect(identity.updated_at).to eq(microsecond_precision_datetime)
end
end
context "when a DateTime field's value is generated by FactoryBot / ActiveRecord" do
let(:identity) { create(:identity) }
it 'returns the same value both before and after reloading the record' do
updated_at_before_reload = identity.updated_at
expect(identity.updated_at).to eq(updated_at_before_reload)
identity.reload
expect(identity.updated_at).to eq(updated_at_before_reload)
end
end
end
Relevant part of the DB schema:
ActiveRecord::Schema[7.0].define do
create_table "identities", id: { type: :string, limit: 12 }, force: :cascade do |t|
t.datetime "updated_at", precision: nil, null: false
Expected behavior
The value for updated_at in the first spec should be at microsecond precision both before and after reloading.
Actual behavior
Calling reload on the record changed the value of the updated_at field.
System configuration
factory_bot version: 6.2.0
rails version: 7.0.8.6
ruby version: 3.3.4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided reproduction specs with the stated FactoryBot, Rails, and Ruby versions, then trace the record creation and reload behavior for the identity's updated_at field. Done means an explicitly over-precise value is truncated to database precision before observation, while FactoryBot/ActiveRecord-generated timestamps remain unchanged across reloads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100