factory_bot
factory_bot copied to clipboard
Build stubbed generate uuids primary keys
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 😄
@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.
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?
Mentioned elsewhere, but thank you @alexandreruban for kicking off this fix.