File tree Expand file tree Collapse file tree 2 files changed +15
-4
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -639,11 +639,12 @@ def default_index_type?(index) # :nodoc:
639
639
end
640
640
641
641
def build_insert_sql ( insert ) # :nodoc:
642
- sql = +"INSERT"
643
- sql << " IGNORE" if insert . skip_duplicates?
644
- sql << " #{ insert . into } #{ insert . values_list } "
642
+ sql = +"INSERT #{ insert . into } #{ insert . values_list } "
645
643
646
- if insert . update_duplicates?
644
+ if insert . skip_duplicates?
645
+ no_op_column = quote_column_name ( insert . keys . first )
646
+ sql << " ON DUPLICATE KEY UPDATE #{ no_op_column } =#{ no_op_column } "
647
+ elsif insert . update_duplicates?
647
648
sql << " ON DUPLICATE KEY UPDATE "
648
649
if insert . raw_update_sql?
649
650
sql << insert . raw_update_sql
Original file line number Diff line number Diff line change @@ -129,6 +129,16 @@ def test_insert_all_can_skip_duplicate_records
129
129
end
130
130
end
131
131
132
+ if current_adapter? ( :Mysql2Adapter , :TrilogyAdapter )
133
+ def test_insert_all_generates_correct_sql
134
+ skip unless supports_insert_on_duplicate_skip?
135
+
136
+ assert_sql ( /ON DUPLICATE KEY UPDATE/ ) do
137
+ Book . insert_all [ { id : 1 , name : "Agile Web Development with Rails" } ]
138
+ end
139
+ end
140
+ end
141
+
132
142
def test_insert_all_with_skip_duplicates_and_autonumber_id_not_given
133
143
skip unless supports_insert_on_duplicate_skip?
134
144
You can’t perform that action at this time.
0 commit comments