Open
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