aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/insert_all.rb
diff options
context:
space:
mode:
authorBob Lail <bob.lailfamily@gmail.com>2019-04-04 07:35:04 -0500
committerBob Lail <bob.lailfamily@gmail.com>2019-04-08 08:01:40 -0500
commitc24efdbd2720bd4d936a3ffab0c22af0a718d2f1 (patch)
treee3f234d04ca0c06847c236b5c959aeb8af1e1953 /activerecord/lib/active_record/insert_all.rb
parentbf1494a1018a0bdc50dac4e87fdbf4b6b03083fa (diff)
downloadrails-c24efdbd2720bd4d936a3ffab0c22af0a718d2f1.tar.gz
rails-c24efdbd2720bd4d936a3ffab0c22af0a718d2f1.tar.bz2
rails-c24efdbd2720bd4d936a3ffab0c22af0a718d2f1.zip
When skipping duplicates in bulk insert on MySQL, avoid assigning id when not specified
If `id` is an `AUTONUMBER` column, then my former strategy here of assigning `no_op_column` to an arbitrary column would fail in this specific scenario: 1. `model.columns.first` is an AUTONUMBER column 2. `model.columns.first` is not assigned in the insert attributes I added three tests: the first test covers the actual error; the second test documents that this _isn't_ a problem when a value is given for the AUTONUMBER column and the third test ensures that this no-op strategy isn't secretly doing an UPSERT.
Diffstat (limited to 'activerecord/lib/active_record/insert_all.rb')
-rw-r--r--activerecord/lib/active_record/insert_all.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/insert_all.rb b/activerecord/lib/active_record/insert_all.rb
index 4b02d40aa0..d7808919ee 100644
--- a/activerecord/lib/active_record/insert_all.rb
+++ b/activerecord/lib/active_record/insert_all.rb
@@ -111,7 +111,7 @@ module ActiveRecord
class Builder
attr_reader :model
- delegate :skip_duplicates?, :update_duplicates?, to: :insert_all
+ delegate :skip_duplicates?, :update_duplicates?, :keys, to: :insert_all
def initialize(insert_all)
@insert_all, @model, @connection = insert_all, insert_all.model, insert_all.connection
@@ -122,7 +122,7 @@ module ActiveRecord
end
def values_list
- types = extract_types_from_columns_on(model.table_name, keys: insert_all.keys)
+ types = extract_types_from_columns_on(model.table_name, keys: keys)
values_list = insert_all.map_key_with_value do |key, value|
bind = Relation::QueryAttribute.new(key, value, types[key])