Merge pull request #65 from jabley/missing-column

Provide more detailed error message
This commit is contained in:
Zach Dennis 2012-09-14 08:00:18 -07:00
commit c08d31b528

View file

@ -19,8 +19,8 @@ module ActiveRecord::Import #:nodoc:
end end
class MissingColumnError < StandardError class MissingColumnError < StandardError
def initialize(index) def initialize(name, index)
super "Missing column for value at index #{index}" super "Missing column for value <#{name}> at index #{index}"
end end
end end
end end
@ -276,7 +276,13 @@ class ActiveRecord::Base
# information on +column_names+, +array_of_attributes_ and # information on +column_names+, +array_of_attributes_ and
# +options+. # +options+.
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} ) def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
columns = column_names.map { |name| columns_hash[name.to_s] } columns = column_names.each_with_index.map do |name, i|
column = columns_hash[name.to_s]
raise ActiveRecord::Import::MissingColumnError.new(name.to_s, i) if column.nil?
column
end
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})" columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES " insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES "
@ -308,8 +314,6 @@ class ActiveRecord::Base
my_values = arr.each_with_index.map do |val,j| my_values = arr.each_with_index.map do |val,j|
column = columns[j] column = columns[j]
raise ActiveRecord::Import::MissingColumnError.new(j) if column.nil?
if val.nil? && !sequence_name.blank? && column.name == primary_key if val.nil? && !sequence_name.blank? && column.name == primary_key
connection.next_value_for_sequence(sequence_name) connection.next_value_for_sequence(sequence_name)
else else