Skip to content

Commit 153985c

Browse files
committed
Reverse order of values when upserting
I contributed two tests to Rails because I was not sure how `insert_all` and `upsert_all` should behave with duplicate identifiers. For `insert_all`, all DBMS only insert the first value. For `upsert_all`, PostgreSQL throws an error in this scenario, but with sqlite and MySQL, the last entry ends up in the database. For now, I do not have a smarter idea than reversing the `inserts`, which ultimately provide the `values_list`. Adding `MAX` would be another, but I fear the SQL query would get much more complicated.
1 parent 2e23939 commit 153985c

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

Diff for: lib/active_record/connection_adapters/sqlserver/database_statements.rb

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def build_insert_sql(insert) # :nodoc:
169169
end
170170

171171
def build_sql_for_merge_insert(insert:, insert_all:, columns_with_uniqueness_constraints:) # :nodoc:
172+
insert_all.inserts.reverse! if insert.update_duplicates?
173+
172174
sql = <<~SQL
173175
MERGE INTO #{insert.model.quoted_table_name} WITH (UPDLOCK, HOLDLOCK) AS target
174176
USING (

0 commit comments

Comments
 (0)