fixed problem with duplicate related objects for Row::new/insert
Luke Saunders [Mon, 22 Oct 2007 19:45:46 +0000 (19:45 +0000)]
Changes
lib/DBIx/Class/Row.pm
t/96multi_create.t
t/lib/DBICTest/Schema/Artist.pm

diff --git a/Changes b/Changes
index 03b10ba..2b81239 100644 (file)
--- 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
index 41c804e..6ac92bf 100644 (file)
@@ -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);
         }
       }
     }
index 4596cda..d898563 100644 (file)
@@ -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 :{
 
index 2221029..ae92606 100644 (file)
@@ -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' );