Model#import fails to find model's primary key sequence

Fixes a bug where Model#import does not work when a model's primary key
has no associated 'sequence' in the database. If a value for the primary
key is specified, then the insert should not require a database
'sequence' to generate a value for that primary key field.
This commit is contained in:
Jared Luxenberg 2012-08-04 17:31:56 -07:00
parent f74bd2fbbd
commit e12f9c4951
4 changed files with 15 additions and 2 deletions

View file

@ -297,7 +297,7 @@ class ActiveRecord::Base
array_of_attributes.map do |arr| array_of_attributes.map do |arr|
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]
if !sequence_name.blank? && column.name == primary_key && val.nil? 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
connection.quote(column.type_cast(val), column) connection.quote(column.type_cast(val), column)

View file

@ -17,4 +17,9 @@ describe "#import" do
assert_equal 1, result.num_inserts assert_equal 1, result.num_inserts
end end
end end
it "should import models whose primary key has no sequence if the primary key's value is specified" do
result = Widget.import Build(3, :widgets)
assert_equal 1, result.num_inserts
end
end end

View file

@ -95,4 +95,8 @@ ActiveRecord::Schema.define do
end end
add_index :animals, [:name], :unique => true, :name => 'uk_animals' add_index :animals, [:name], :unique => true, :name => 'uk_animals'
create_table :widgets, :id => false, :force => true do |t|
t.integer :w_id
end
end end

View file

@ -11,3 +11,7 @@ Factory.define :topic do |m|
m.sequence(:title){ |n| "Title #{n}"} m.sequence(:title){ |n| "Title #{n}"}
m.sequence(:author_name){ |n| "Author #{n}"} m.sequence(:author_name){ |n| "Author #{n}"}
end end
Factory.define :widget do |m|
m.sequence(:w_id){ |n| n}
end