From: Matt S Trout Date: Fri, 23 Sep 2005 14:59:58 +0000 (+0000) Subject: More fixes to HasA/BelongsTo courtesy of abraxxa X-Git-Tag: v0.03001~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0200ec9a0bc45cd04f19cb59bf2e67f51d674734;p=dbsrgits%2FDBIx-Class.git More fixes to HasA/BelongsTo courtesy of abraxxa --- diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index 3010954..e35c221 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -19,9 +19,6 @@ sub has_a { $self->inflate_column($col, \%args); return 1; } - my ($pri, $too_many) = keys %{ $f_class->_primaries }; - $self->throw( "has_a only works with a single primary key; ${f_class} has more. try using a belongs_to relationship instead of Class::DBI compat rels" ) - if $too_many; $self->belongs_to($col, $f_class); return 1; diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 4201835..35e5428 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -6,9 +6,13 @@ use warnings; sub belongs_to { my ($class, $rel, $f_class, $cond, $attrs) = @_; eval "require $f_class"; + my %f_primaries = eval { %{ $f_class->_primaries } }; + my $f_loaded = !$@; # single key relationship if (not defined $cond) { - my ($pri, $too_many) = keys %{ $f_class->_primaries }; + $class->throw("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") unless $f_loaded; + my ($pri, $too_many) = keys %f_primaries; + $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") if $too_many; my $acc_type = ($class->_columns->{$rel}) ? 'filter' : 'single'; $class->add_relationship($rel, $f_class, { "foreign.${pri}" => "self.${rel}" }, @@ -17,8 +21,6 @@ sub belongs_to { } # multiple key relationship else { - my %f_primaries = eval { %{ $f_class->_primaries } }; - my $f_loaded = !$@; my $cond_rel; for (keys %$cond) { if (m/\./) { # Explicit join condition @@ -33,7 +35,7 @@ sub belongs_to { $class->throw("non primary key used in join condition: $_"); } } - $class->throw("not all primary keys used in multi key relationship!") if $f_loaded && keys %f_primaries; + $class->throw("Invalid belongs_to specification for ${rel} on ${class}; primary key columns ".join(', ', keys %f_primaries)." of ${f_class} not specified in join condition") if ($f_loaded && keys %f_primaries); $class->add_relationship($rel, $f_class, $cond_rel, { accessor => 'single', %{$attrs ||{}} }