$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;
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}" },
}
# 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
$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 ||{}} }