factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

Updating from 6.2.0 -> 6.4.2 is a breaking change for us: "Do not define "id". Instead, rely on the database to generate it."

Open MarcusRiemer opened this issue 1 year ago • 7 comments

Description

We are modeling tree structures in with UUID primary keys. We specifically chose UUID as type because among other benefits we can generate those IDs in the application and send whole trees to the database in a single transaction without having to go back and forth for each new node we are inserting.

Reproduction Steps

We have this factory:

factory :root_node, class: 'Tree' do
  id { SecureRandom.uuid }
  root_id { id }
end

Expected behavior

The instance should be constructed with identical values for id and root_id (as was the case with FactoryBot 6.2.0). Alternatively we would be fine with overriding this behavior but browsing the Readme or the web for "Do not define "id". Instead, rely on the database to generate it." didn't yield any helpful results.

Actual behavior

Creating instances worked just fine with FactoryBot 6.2.0, but since at latest 6.4.2 this results in the following error:

     FactoryBot::AttributeDefinitionError:
       Attribute generates "id" primary key for Tree"
       Do not define "id". Instead, rely on the database to generate it.

System configuration

factory_bot version: 6.4.2 rails version: 7.0.8 ruby version: 3.2.2

MarcusRiemer avatar Nov 24 '23 11:11 MarcusRiemer

https://github.com/thoughtbot/factory_bot_rails/blob/54c72541ac8d9afa2c94274d80597c24112ce054/README.md?plain=1#L168C1-L168C57 should help.

pyromaniac avatar Nov 27 '23 02:11 pyromaniac

Within the context @pyromaniac, I think we can close this "bug". It's more like a configurable feature.

p.s. this feature was a surprise to me, too.

StanBright avatar Nov 27 '23 23:11 StanBright

I'd suggest a more prominent announcement, e.g., at the top of the README, to close this one out. Call it an inadvertent breaking change and point to the docs for how to get the old behavior.

BiggerNoise avatar Nov 28 '23 17:11 BiggerNoise

I'm happy that there is a feasible way to revert back to the old behavior and this has addressed my personal needs. I do however agree with @BiggerNoise : a more prominent mention would be very helpful. What about either:

  • Mentioning the config value in the error message?
  • Mention the error message in the Readme?

MarcusRiemer avatar Nov 28 '23 21:11 MarcusRiemer

Agree that being able to revert to the previous behavior has met my needs as well.

I think @MarcusRiemer documentation suggestions would be very helpful, would a P/R be helpful?

BiggerNoise avatar Nov 28 '23 21:11 BiggerNoise

One issue with turning off via config.factory_bot.reject_primary_key_attributes = false is that for Rails engines this needs to go in the dummy app, not in say spec_helper.

krisleech avatar Nov 29 '23 14:11 krisleech

Seems like the kind of breaking change that should warrant a major version number increment.

jagthedrummer avatar Nov 29 '23 18:11 jagthedrummer