Remove the dubious primary keys check. Not clear it's useful or valid.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / BelongsTo.pm
1 package DBIx::Class::Relationship::BelongsTo;
2
3 use strict;
4 use warnings;
5
6 sub belongs_to {
7   my ($class, $rel, $f_class, $cond, $attrs) = @_;
8   eval "require $f_class";
9   my %f_primaries = eval { %{ $f_class->_primaries } };
10   my $f_loaded = !$@;
11   # single key relationship
12   if (not defined $cond) {
13     $class->throw("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") unless $f_loaded;
14     my ($pri, $too_many) = keys %f_primaries;
15     $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") if $too_many;
16     my $acc_type = ($class->_columns->{$rel}) ? 'filter' : 'single';
17     $class->add_relationship($rel, $f_class,
18       { "foreign.${pri}" => "self.${rel}" },
19       { accessor => $acc_type, %{$attrs || {}} }
20     );
21   }
22   # multiple key relationship
23   else {
24     my $cond_rel;
25     for (keys %$cond) {
26       if (m/\./) { # Explicit join condition
27         $cond_rel = $cond;
28         last;
29       }
30       $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
31     }
32     $class->add_relationship($rel, $f_class,
33       $cond_rel,
34       { accessor => 'single', %{$attrs || {}} }
35     );
36   }
37   return 1;
38 }
39
40 =head1 AUTHORS
41
42 Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
43
44 Matt S. Trout <mst@shadowcatsystems.co.uk>
45
46 =cut
47
48 1;