Repository navigation
Identify a uuid primary key by its Active Record type - #1838
Open
IslamElsayed wants to merge 2 commits into
Open
IslamElsayed wants to merge 2 commits into
IslamElsayed wants to merge 2 commits into
Conversation
build_stubbed assigns an id from an integer sequence unless the primary key is a uuid, which it decided by comparing the column's sql_type to "uuid". That is PostgreSQL's name for it. SQL Server calls the same thing "uniqueidentifier", so the check failed there, the id came from the integer sequence, and assigning an Integer to a uuid attribute left it nil -- the symptom reported in thoughtbot#1618. Adapters disagree on the name but agree on the type: the SQL Server adapter registers "uniqueidentifier" as SQLServer::Type::Uuid, whose #type is :uuid. Ask the column for its Active Record type first and keep the sql_type comparison for anything that reports the name without mapping the type. Closes thoughtbot#1618
IslamElsayed
requested review from
neilvcarvalho and
vburzynski
as code owners
September 16, 2026 11:35
Author
|
Friendly ping on this one. CI hasn't run yet because it's waiting for first-time contributor approval. Could a maintainer approve the workflows when there's a moment? Happy to make any changes. (I used an AI assistant while preparing this PR, and I reviewed and tested the change myself.) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1618.
build_stubbedpicks the id from an integer sequence unless the primary key is a uuid, and it decided that by comparing the column'ssql_typeto"uuid":That is PostgreSQL's name for the type. SQL Server calls the same thing
uniqueidentifier, so the check failed,next_idfell through to@@next_id += 1, and assigning anIntegerto a uuid attribute leftidnil — the symptom in the issue.The adapters disagree on the name but agree on the Active Record type. From the SQL Server adapter's own source:
So this asks the column for
typefirst and keeps thesql_typecomparison as a fallback for anything that reports the name without mapping the type.Testing
Three examples in
spec/factory_bot/strategy/stub_spec.rb, driven by a column double so they need no SQL Server::uuid/ sql_type"uniqueidentifier"→ uuid id (the reported case)"uuid"with notype→ uuid id (the existing PostgreSQL path):integer→ still an Integer idI checked they are not vacuous by breaking the fix both ways: reverting to the
sql_type-only check fails the first, and dropping thesql_typebranch fails the second. The existingspec/acceptance/stub_spec.rbuuid examples still pass unchanged.Full suite: 777 examples, 0 failures.
standardrbclean on both files. Run under Ruby 3.3.5 rather than the 4.0.1 in.tool-versions, since the gemspec allows>= 3.0.0.I do not have SQL Server to hand, so the adapter behaviour above is read from its source rather than executed — correct me if
uniqueidentifierreachescolumn_for_attributedifferently in practice.Disclosure: I use AI assistance when working on patches, and I check and test everything before submitting.