diff options
author | Josef Šimánek <josef.simanek@gmail.com> | 2019-03-16 02:33:48 +0000 |
---|---|---|
committer | Josef Šimánek <josef.simanek@gmail.com> | 2019-03-16 14:53:01 +0000 |
commit | 6eb3f5e8f5fb41c2a62ceca13ac5d75835a55fb5 (patch) | |
tree | 5afd680494d479d283a2074b499514d24414e404 /activerecord | |
parent | 26b5e6284703432fc9022b215a221037fc05920d (diff) | |
download | rails-6eb3f5e8f5fb41c2a62ceca13ac5d75835a55fb5.tar.gz rails-6eb3f5e8f5fb41c2a62ceca13ac5d75835a55fb5.tar.bz2 rails-6eb3f5e8f5fb41c2a62ceca13ac5d75835a55fb5.zip |
Raise UnknownAttributeError when unknown column is passed to insert_all and friends.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/insert_all.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/insert_all_test.rb | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/insert_all.rb b/activerecord/lib/active_record/insert_all.rb index 5baaea344b..3833cb2fcf 100644 --- a/activerecord/lib/active_record/insert_all.rb +++ b/activerecord/lib/active_record/insert_all.rb @@ -92,7 +92,15 @@ module ActiveRecord def values_list columns = connection.schema_cache.columns_hash(model.table_name) + + column_names = columns.keys.to_set keys = insert_all.keys.to_set + unknown_columns = keys - column_names + + unless unknown_columns.empty? + raise UnknownAttributeError.new(model.new, unknown_columns.first) + end + types = keys.map { |key| [ key, connection.lookup_cast_type_from_column(columns[key]) ] }.to_h values_list = insert_all.inserts.map do |attributes| diff --git a/activerecord/test/cases/insert_all_test.rb b/activerecord/test/cases/insert_all_test.rb index 63245719c0..a3e920969d 100644 --- a/activerecord/test/cases/insert_all_test.rb +++ b/activerecord/test/cases/insert_all_test.rb @@ -136,4 +136,10 @@ class InsertAllTest < ActiveRecord::TestCase assert_equal ["Out of the Silent Planet", "Perelandra"], Book.where(isbn: "1974522598").order(:name).pluck(:name) end + + def test_insert_all_raises_on_unknown_attribute + assert_raise ActiveRecord::UnknownAttributeError do + Book.insert_all! [{ unknown_attribute: "Test" }] + end + end end |