Fix UUID primary key not returned after INSERT following Rails insert API change - #1394
Draft
aidanharan with Copilot wants to merge 2 commits into
Draft
Fix UUID primary key not returned after INSERT following Rails insert API change#1394aidanharan with Copilot wants to merge 2 commits into
aidanharan with Copilot wants to merge 2 commits into
Conversation
In Rails main (commit 3eae6ecea7), `_insert_record` no longer passes the `pk` argument to `connection.insert`, so `pk` is always nil in `sql_for_insert`. Previously `sql_for_insert` would detect pk from `query_requires_identity_insert?` (only works for IDENTITY columns), but UUID and non-identity primary key tables returned false. Fix: When pk is still nil after the identity insert check and `use_output_inserted?` is true, fall back to detecting the table name with `get_table_name` and looking up pk from the schema cache. This ensures `OUTPUT INSERTED.[pk]` is correctly generated for UUID pk tables and trigger tables with UUID pks.
Copilot
AI
changed the title
[WIP] Fix issues with Copilot integration
Fix UUID primary key not returned after INSERT following Rails insert API change
Aug 1, 2026
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.
Rails main (commit
3eae6ecea7) removed thepkpositional argument from_insert_record's call toconnection.insert, sopkarrives asnilinsql_for_insert. The previous fallback usedquery_requires_identity_insert?, which only returns a table name for IDENTITY (auto-increment) columns — for UUID primary keys it returnsfalse, leavingpknil. The conditionif pk && use_output_inserted?then failed, appendingSCOPE_IDENTITY()instead ofOUTPUT INSERTED.[id], which returnsNULLfor non-identity tables and sets the record'sidtonilafter create.Changes
sql_for_insertindatabase_statements.rb: Whenpkis stillnilafter the identity-insert detection pass, add a second lookup that callsget_table_name(sql)to parse the table from the SQL string, then resolves primary keys viaschema_cache.primary_keys. This is gated onuse_output_inserted?to avoid the extra work on the fallback (SCOPE_IDENTITY) path.Fixes failing tests:
SQLServerUuidTest#test_0002,SQLServerUuidTest#test_0003,SQLServerTriggerTest#test_0002(UUID trigger), andSQLServerTriggerTest#test_0004(composite PK with UUID+int, trigger).