Skip to content

Fix UUID primary key not returned after INSERT following Rails insert API change - #1394

Draft
aidanharan with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-with-copilot
Draft

Fix UUID primary key not returned after INSERT following Rails insert API change#1394
aidanharan with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-with-copilot

Conversation

Copilot AI commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Rails main (commit 3eae6ecea7) removed the pk positional argument from _insert_record's call to connection.insert, so pk arrives as nil in sql_for_insert. The previous fallback used query_requires_identity_insert?, which only returns a table name for IDENTITY (auto-increment) columns — for UUID primary keys it returns false, leaving pk nil. The condition if pk && use_output_inserted? then failed, appending SCOPE_IDENTITY() instead of OUTPUT INSERTED.[id], which returns NULL for non-identity tables and sets the record's id to nil after create.

Changes

  • sql_for_insert in database_statements.rb: When pk is still nil after the identity-insert detection pass, add a second lookup that calls get_table_name(sql) to parse the table from the SQL string, then resolves primary keys via schema_cache.primary_keys. This is gated on use_output_inserted? to avoid the extra work on the fallback (SCOPE_IDENTITY) path.
# Before — only worked for IDENTITY columns; UUID tables left pk = nil
if pk.nil?
  table_name = query_requires_identity_insert?(sql)
  pk = schema_cache.primary_keys(table_name)  # table_name is `false` for UUID tables
end

# After — falls back to parsing the table from SQL when needed
if pk.nil?
  table_name = query_requires_identity_insert?(sql)
  pk = schema_cache.primary_keys(table_name) if table_name
end

if pk.nil? && use_output_inserted? && !database_prefix_remote_server?
  table_name ||= get_table_name(sql)
  pk = schema_cache.primary_keys(table_name) if table_name
end

Fixes failing tests: SQLServerUuidTest#test_0002, SQLServerUuidTest#test_0003, SQLServerTriggerTest#test_0002 (UUID trigger), and SQLServerTriggerTest#test_0004 (composite PK with UUID+int, trigger).

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
Copilot AI requested a review from aidanharan August 1, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants