Skip to content

Handle insert returning using symbol #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def build_insert_sql(insert) # :nodoc:
returning_sql = if returning.is_a?(String)
returning
else
returning.map { |column| "INSERTED.#{quote_column_name(column)}" }.join(", ")
Array(returning).map { |column| "INSERTED.#{quote_column_name(column)}" }.join(", ")
end
sql << " OUTPUT #{returning_sql}"
end
Expand Down
11 changes: 11 additions & 0 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,17 @@ def test_insert_all_returns_requested_sql_fields_coerced
result = Book.insert_all! [{ name: "Rework", author_id: 1 }], returning: Arel.sql("UPPER(INSERTED.name) as name")
assert_equal %w[ REWORK ], result.pluck("name")
end

# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
coerce_tests! :test_insert_with_type_casting_and_serialize_is_consistent
def test_insert_with_type_casting_and_serialize_is_consistent_coerced
connection.remove_index(:books, column: [:author_id, :name])

original_test_insert_with_type_casting_and_serialize_is_consistent
ensure
Book.where(author_id: nil, name: '["Array"]').delete_all
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
end
end

module ActiveRecord
Expand Down
Loading