1 package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
10 our %_pod_inherit_config =
12 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
16 shift->_has_one('LEFT' => @_);
20 shift->_has_one(undef() => @_);
24 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
26 $class->ensure_class_loaded($f_class);
28 my $pri = $class->result_source_instance->_single_pri_col_or_die;
30 my $f_class_loaded = try { $f_class->columns };
32 if (defined $cond && length $cond) {
34 $guess = "caller specified foreign key '$f_key'";
35 } elsif ($f_class_loaded && $f_class->has_column($rel)) {
37 $guess = "using given relationship name '$rel' as foreign key column name";
38 } elsif ($f_class_loaded and my $f_pri = try {
39 $f_class->result_source_instance->_single_pri_col_or_die
42 $guess = "using primary key of foreign class for foreign key";
45 $class->throw_exception(
46 "No such column '$f_key' on foreign class ${f_class} ($guess)"
47 ) if $f_class_loaded && !$f_class->has_column($f_key);
49 $cond = { "foreign.${f_key}" => "self.${pri}" };
51 $class->_validate_has_one_condition($cond);
53 my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
55 $class->add_relationship($rel, $f_class,
57 { accessor => 'single',
58 cascade_update => $default_cascade,
59 cascade_delete => $default_cascade,
60 ($join_type ? ('join_type' => $join_type) : ()),
65 sub _validate_has_one_condition {
66 my ($class, $cond ) = @_;
68 return if $ENV{DBIC_DONT_VALIDATE_RELS};
69 return unless 'HASH' eq ref $cond;
70 foreach my $foreign_id ( keys %$cond ) {
71 my $self_id = $cond->{$foreign_id};
73 # we can ignore a bad $self_id because add_relationship handles this
75 return unless $self_id =~ /^self\.(.*)$/;
77 $class->throw_exception("Defining rel on ${class} that includes '$key' but no such column defined here yet")
78 unless $class->has_column($key);
79 my $column_info = $class->column_info($key);
80 if ( $column_info->{is_nullable} ) {
81 carp(qq'"might_have/has_one" must not be on columns with is_nullable set to true ($class/$key). This might indicate an incorrect use of those relationship helpers instead of belongs_to.');