# single key relationship
if (!ref $cond) {
- my ($pri,$too_many);
- if (!defined $cond) {
- $class->throw("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") unless $f_loaded;
- ($pri, $too_many) = keys %f_primaries;
- $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys") unless defined $pri;
- $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") if $too_many;
- }
- else {
- $pri = $cond;
- }
- my $acc_type = ($class->has_column($rel)) ? 'filter' : 'single';
+ $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 no primary keys")
+ unless defined $pri;
+ $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key")
+ if $too_many;
+
+ my $fk = defined $cond ? $cond : $rel;
+ $class->throw("Can't infer join condition for ${rel} on ${class}; $fk is not a column")
+ unless $class->has_column($fk);
+
+ my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
$class->add_relationship($rel, $f_class,
- { "foreign.${pri}" => "self.${rel}" },
+ { "foreign.${pri}" => "self.${fk}" },
{ accessor => $acc_type, %{$attrs || {}} }
);
}
DBICTest::Schema::Tag->belongs_to('cd', 'DBICTest::Schema::CD');
-DBICTest::Schema::Track->belongs_to('cd', 'DBICTest::Schema::CD', 'cdid');
+DBICTest::Schema::Track->belongs_to('cd', 'DBICTest::Schema::CD');
+
+DBICTest::Schema::Track->belongs_to('disc', 'DBICTest::Schema::CD', 'cd');
DBICTest::Schema::TwoKeys->belongs_to('artist', 'DBICTest::Schema::Artist');
$track->set_from_related( cd => $cd );
if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
- is($track->cd->cdid, 4, 'set_from_related ok' );
+ is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
} else {
is( $track->cd, 4, 'set_from_related ok' );
}