[Dbix-class] patch for ResultSet::find_or_new

Patrick Weemeeuw pweemeeuw at telenet.be
Sun Jan 13 09:41:43 GMT 2008


There is a small bug in find_or_new: when the find part
fails, it calls new_result with the hash containing the
values that were used for the search. It should use no
values at all instead.

Example buggy case: $o = $...->find_or_new( { id => $id } )
with id a not null primary key. When $id is undefined, there
is obviously no row in the DB, and a new result object is
returned. However, the object returned contains the column
id => NULL, which (1) is invalid for this kind of object,
and (2) prevents in some backends (e.g. Pg) that the
sequence is used to generate a unique id.


$ diff -U 5 ResultSet.pm.orig ResultSet.pm
--- ResultSet.pm.orig	2007-10-31 22:08:51.000000000 +0100
+++ ResultSet.pm	2008-01-12 09:25:52.000000000 +0100
@@ -1552,11 +1552,11 @@
 sub find_or_new {
   my $self     = shift;
   my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
   my $hash     = ref $_[0] eq 'HASH' ? shift : {@_};
   my $exists   = $self->find($hash, $attrs);
-  return defined $exists ? $exists : $self->new_result($hash);
+  return defined $exists ? $exists : $self->new_result({});
 }
 
 =head2 create
 
 =over 4



More information about the DBIx-Class mailing list