Fold column_info() into columns_info()
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
22b15c96 3
4use strict;
5use warnings;
70c28808 6use DBIx::Class::Carp;
ed7ab0f4 7use Try::Tiny;
ddcc02d1 8use DBIx::Class::_Util 'dbic_internal_try';
fd323bf1 9use namespace::clean;
22b15c96 10
fd323bf1 11our %_pod_inherit_config =
044e70c7 12 (
13 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
14 );
15
07037f89 16sub might_have {
17 shift->_has_one('LEFT' => @_);
18}
19
22b15c96 20sub has_one {
e5d98edd 21 shift->_has_one(undef() => @_);
07037f89 22}
23
24sub _has_one {
503536d5 25 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
99be059e 26 unless (ref $cond) {
0b0743af 27 my $pri = $class->result_source_instance->_single_pri_col_or_die;
e36de82e 28
9e7525a2 29 my ($f_key,$guess,$f_rsrc);
dcf8330b 30 if (defined $cond && length $cond) {
99be059e 31 $f_key = $cond;
dcf8330b 32 $guess = "caller specified foreign key '$f_key'";
22b15c96 33 }
9e7525a2 34 else {
35 # at this point we need to load the foreigner, expensive or not
36 $class->ensure_class_loaded($f_class);
37
ddcc02d1 38 $f_rsrc = dbic_internal_try {
38fe1ff9 39 my $r = $f_class->result_source_instance;
40 die "There got to be some columns by now... (exception caught and rewritten by catch below)"
41 unless $r->columns;
42 $r;
9e7525a2 43 }
44 catch {
45 $class->throw_exception(
46 "Foreign class '$f_class' does not seem to be a Result class "
47 . "(or it simply did not load entirely due to a circular relation chain)"
48 );
49 };
0b0743af 50
9e7525a2 51 if ($f_rsrc->has_column($rel)) {
52 $f_key = $rel;
53 $guess = "using given relationship name '$rel' as foreign key column name";
54 }
55 else {
56 $f_key = $f_rsrc->_single_pri_col_or_die;
57 $guess = "using primary key of foreign class for foreign key";
58 }
59 }
60
38fe1ff9 61# FIXME - this check needs to be moved to schema-composition time...
62# # only perform checks if the far side was not preloaded above *AND*
63# # appears to have been loaded by something else (has a rsrc_instance)
ddcc02d1 64# if (! $f_rsrc and $f_rsrc = dbic_internal_try { $f_class->result_source_instance }) {
38fe1ff9 65# $class->throw_exception(
66# "No such column '$f_key' on foreign class ${f_class} ($guess)"
67# ) if !$f_rsrc->has_column($f_key);
68# }
0b0743af 69
503536d5 70 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 71 }
2339d6c7 72 $class->_validate_has_one_condition($cond);
edcecdbb 73
74 my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
75
07037f89 76 $class->add_relationship($rel, $f_class,
77 $cond,
503536d5 78 { accessor => 'single',
edcecdbb 79 cascade_update => $default_cascade,
80 cascade_delete => $default_cascade,
29d57632 81 is_depends_on => 0,
07037f89 82 ($join_type ? ('join_type' => $join_type) : ()),
503536d5 83 %{$attrs || {}} });
07037f89 84 1;
22b15c96 85}
86
2339d6c7 87sub _validate_has_one_condition {
dc571b76 88 my ($class, $cond ) = @_;
89
90 return if $ENV{DBIC_DONT_VALIDATE_RELS};
91 return unless 'HASH' eq ref $cond;
92 foreach my $foreign_id ( keys %$cond ) {
93 my $self_id = $cond->{$foreign_id};
94
95 # we can ignore a bad $self_id because add_relationship handles this
6909ab3c 96 # exception
dc571b76 97 return unless $self_id =~ /^self\.(.*)$/;
6909ab3c 98
dc571b76 99 my $key = $1;
b83736a7 100
101 my $column_info = $class->result_source->columns_info->{$key}
102 or $class->throw_exception(
103 "Defining rel on ${class} that includes '$key' "
104 . 'but no such column defined there yet'
105 );
106
107 carp(
108 "'might_have'/'has_one' must not be used on columns with is_nullable "
109 . "set to true ($class/$key). This almost certainly indicates an "
110 . "incorrect use of these relationship helpers instead of 'belongs_to'"
111 ) if $column_info->{is_nullable};
dc571b76 112 }
113}
114
22b15c96 1151;