1 package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
7 our %_pod_inherit_config =
9 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
13 shift->_has_one('LEFT' => @_);
17 shift->_has_one(undef() => @_);
21 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
23 $class->ensure_class_loaded($f_class);
24 my ($pri, $too_many) = $class->primary_columns;
26 $class->throw_exception(
27 "might_have/has_one can only infer join for a single primary key; ".
31 $class->throw_exception(
32 "might_have/has_one needs a primary key to infer a join; ".
34 ) if !defined $pri && (!defined $cond || !length $cond);
36 my $f_class_loaded = eval { $f_class->columns };
38 if (defined $cond && length $cond) {
40 $guess = "caller specified foreign key '$f_key'";
41 } elsif ($f_class_loaded && $f_class->has_column($rel)) {
43 $guess = "using given relationship '$rel' for foreign key";
45 ($f_key, $too_many) = $f_class->primary_columns;
46 $class->throw_exception(
47 "might_have/has_one can only infer join for a single primary key; ".
50 $guess = "using primary key of foreign class for foreign key";
52 $class->throw_exception(
53 "No such column ${f_key} on foreign class ${f_class} ($guess)"
54 ) if $f_class_loaded && !$f_class->has_column($f_key);
55 $cond = { "foreign.${f_key}" => "self.${pri}" };
57 $class->add_relationship($rel, $f_class,
59 { accessor => 'single',
60 cascade_update => 1, cascade_delete => 1,
61 ($join_type ? ('join_type' => $join_type) : ()),