Throw clearer exception on ->new_related() with a non-existent relationship
Dagfinn Ilmari Mannsåker [Tue, 1 Oct 2013 19:04:22 +0000 (20:04 +0100)]
Changes
lib/DBIx/Class/Relationship/Base.pm
t/relationship/core.t

diff --git a/Changes b/Changes
index d42f06f..11dfb01 100644 (file)
--- a/Changes
+++ b/Changes
@@ -12,6 +12,8 @@ Revision history for DBIx::Class
         - Back out self-cleaning from DBIx::Class::Carp for the time being
           (as a side effect fixes RT#86267)
         - Tests no longer fail if $ENV{DBI_DSN} is set
+        - Throw clearer exception on ->new_related() with a non-existent
+          relationship.
 
     * Misc
         - Replace $row with $result in all docs to be consistent and to
index 23ce915..20a9c17 100644 (file)
@@ -640,8 +640,10 @@ sub new_related {
   if (ref $self) {  # cdbi calls this as a class method, /me vomits
 
     my $rsrc = $self->result_source;
+    my $rel_info = $rsrc->relationship_info($rel)
+      or $self->throw_exception( "No such relationship '$rel'" );
     my (undef, $crosstable, $cond_targets) = $rsrc->_resolve_condition (
-      $rsrc->relationship_info($rel)->{cond}, $rel, $self, $rel
+      $rel_info->{cond}, $rel, $self, $rel
     );
 
     $self->throw_exception("Custom relationship '$rel' does not resolve to a join-free condition fragment")
index 23d829d..e86dfc6 100644 (file)
@@ -136,6 +136,10 @@ lives_ok(
     'No back rel'
 );
 
+throws_ok {
+    my $new_bookmark = $schema->resultset("Bookmark")->new_result( {} );
+    $new_bookmark->new_related( no_such_rel => {} );
+} qr/No such relationship 'no_such_rel'/, 'creating in uknown rel throws';
 
 {
   local $TODO = "relationship checking needs fixing";