factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

Build stubbed generate uuids primary keys

Open alexandreruban opened this issue 4 years ago • 2 comments

Closes #1498

When the primary key of a table has a type of uuid and when using the build_stubbed strategy, the id was not set properly.

This PR checks if the primary key of the result_instance is an uuid. If so, we use SecureRandom.uuid instead of an Integer to generate the id of the model.

This is my first contribution to FactoryBot so please tell me if I did anything wrong 😄

alexandreruban avatar Sep 23 '21 21:09 alexandreruban

@composerinteralia thank you very much for your review! I updated the PR according to your comments.

I don't think sql_type is part of the public rails api, but I'm not sure of another way to get this.

Yes you are right, ActiveRecord::ConnectionAdapters::Column, ActiveRecord::ConnectionAdapters::Postgresql::Column, and ActiveRecord::ConnectionAdapters::MySQL::Column that implement sql_type are not documented, should we still use them? I don't know of another way to get this information either.

alexandreruban avatar Sep 24 '21 09:09 alexandreruban

Using private API involves some amount of risk that the private method will change unexpectedly, but we have taken that risk elsewhere in factory_bot (defined_enums, for example). sql_type seems fairly stable, so I think I'm OK with it. I'll leave this open for a bit to see if any other maintainers have different opinions.

It's sort of awkward that column_for_attribute is public and returns a connection adapter column, but then none of the methods on the column are public. What good is the method if you can't do anything with the return value?

composerinteralia avatar Sep 24 '21 12:09 composerinteralia

Mentioned elsewhere, but thank you @alexandreruban for kicking off this fix.

mike-burns avatar Oct 20 '23 21:10 mike-burns