From: Guillermo Roditi Date: Thu, 12 Feb 2009 01:49:26 +0000 (+0000) Subject: fixed bug for undef_on_null_fk edge case X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=981299aaf0b82c089376356532bda9378bfca6cb;p=dbsrgits%2FDBIx-Class-Historic.git fixed bug for undef_on_null_fk edge case --- diff --git a/Changes b/Changes index 24cd9c6..85f6076 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class - Possible to set locale in IC::DateTime extra => {} config - + - Calling the accessor of a belongs_to when the foreign_key + was NULL and the row was not stored would unexpectedly fail (groditi) 0.08099_06 2009-01-23 07:30:00 (UTC) - Allow a scalarref to be supplied to the 'from' resultset attribute - Classes submitted as result_class for a resultsource are now diff --git a/lib/DBIx/Class/Relationship/Accessor.pm b/lib/DBIx/Class/Relationship/Accessor.pm index dcb906e..6ec2f25 100644 --- a/lib/DBIx/Class/Relationship/Accessor.pm +++ b/lib/DBIx/Class/Relationship/Accessor.pm @@ -31,6 +31,7 @@ sub add_relationship_accessor { $rel_info->{cond}, $rel, $self ); if ($rel_info->{attrs}->{undef_on_null_fk}){ + return unless ref($cond) eq 'HASH'; return if grep { not defined } values %$cond; } my $val = $self->find_related($rel, {}, {}); diff --git a/t/66relationship.t b/t/66relationship.t index fe0196c..7f05be4 100644 --- a/t/66relationship.t +++ b/t/66relationship.t @@ -8,7 +8,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 74; +plan tests => 75; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -228,7 +228,10 @@ eval{ $undef_artist_cd->related_resultset('artist')->new({name => 'foo'}); }; is( $@, '', "Object created on a resultset related to not yet inserted object"); - +lives_ok{ + $schema->resultset('Artwork')->new_result({})->cd; +} 'undef_on_null_fk does not choke on empty conds'; + my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef }); is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded'); is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');