From: Luke Saunders Date: Mon, 22 Oct 2007 19:45:46 +0000 (+0000) Subject: fixed problem with duplicate related objects for Row::new/insert X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c193d1d2d7138948d5b5d8b2b7ae19e12edb2cb0;p=dbsrgits%2FDBIx-Class-Historic.git fixed problem with duplicate related objects for Row::new/insert --- diff --git a/Changes b/Changes index 03b10ba..2b81239 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,5 @@ Revision history for DBIx::Class - + - Row::insert will now not fall over if passed duplicate related objects - When adding relationships, it will throw an exception if you get the foreign and self parts the wrong way round in the condition - ResultSetColumn::func() now returns all results if called in list diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 41c804e..6ac92bf 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -243,7 +243,7 @@ sub insert { my $reverse = $source->reverse_relationship_info($relname); foreach my $obj (@cands) { $obj->set_from_related($_, $self) for keys %$reverse; - $obj->insert() if(!$obj->in_storage); + $obj->insert() unless ($obj->in_storage || $obj->result_source->resultset->search({$obj->get_columns})->count); } } } diff --git a/t/96multi_create.t b/t/96multi_create.t index 4596cda..d898563 100644 --- a/t/96multi_create.t +++ b/t/96multi_create.t @@ -51,6 +51,23 @@ my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3' is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create'); +my $artist2 = $schema->resultset('Artist')->create({ artistid => 1000, + name => 'Fred 3', + cds => [ + { artist => 1000, + title => 'Music to code by', + year => 2007, + }, + ], + cds_unordered => [ + { artist => 1000, + title => 'Music to code by', + year => 2007, + }, + ] + }); + +is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay'); CREATE_RELATED1 :{ diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 2221029..ae92606 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -31,6 +31,9 @@ __PACKAGE__->has_many( cds => 'DBICTest::Schema::CD', undef, { order_by => 'year' }, ); +__PACKAGE__->has_many( + cds_unordered => 'DBICTest::Schema::CD' +); __PACKAGE__->has_many( twokeys => 'DBICTest::Schema::TwoKeys' ); __PACKAGE__->has_many( onekeys => 'DBICTest::Schema::OneKey' );