Reorder relationship sanity checks, run them only on known loaded classes
[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;
fd323bf1 8use namespace::clean;
22b15c96 9
fd323bf1 10our %_pod_inherit_config =
044e70c7 11 (
12 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
13 );
14
07037f89 15sub might_have {
16 shift->_has_one('LEFT' => @_);
17}
18
22b15c96 19sub has_one {
e5d98edd 20 shift->_has_one(undef() => @_);
07037f89 21}
22
23sub _has_one {
503536d5 24 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
99be059e 25 unless (ref $cond) {
0b0743af 26 my $pri = $class->result_source_instance->_single_pri_col_or_die;
e36de82e 27
9e7525a2 28 my ($f_key,$guess,$f_rsrc);
dcf8330b 29 if (defined $cond && length $cond) {
99be059e 30 $f_key = $cond;
dcf8330b 31 $guess = "caller specified foreign key '$f_key'";
22b15c96 32 }
9e7525a2 33 else {
34 # at this point we need to load the foreigner, expensive or not
35 $class->ensure_class_loaded($f_class);
36
37 $f_rsrc = try {
38 $f_class->result_source_instance;
39 }
40 catch {
41 $class->throw_exception(
42 "Foreign class '$f_class' does not seem to be a Result class "
43 . "(or it simply did not load entirely due to a circular relation chain)"
44 );
45 };
0b0743af 46
9e7525a2 47 if ($f_rsrc->has_column($rel)) {
48 $f_key = $rel;
49 $guess = "using given relationship name '$rel' as foreign key column name";
50 }
51 else {
52 $f_key = $f_rsrc->_single_pri_col_or_die;
53 $guess = "using primary key of foreign class for foreign key";
54 }
55 }
56
57 # only perform checks if the far side was not preloaded above *AND*
58 # appears to have been loaded by something else (has a rsrc_instance)
59 if (! $f_rsrc and $f_rsrc = try { $f_class->result_source_instance }) {
60 $class->throw_exception(
61 "No such column '$f_key' on foreign class ${f_class} ($guess)"
62 ) if !$f_rsrc->has_column($f_key);
63 }
0b0743af 64
503536d5 65 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 66 }
2339d6c7 67 $class->_validate_has_one_condition($cond);
edcecdbb 68
69 my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
70
07037f89 71 $class->add_relationship($rel, $f_class,
72 $cond,
503536d5 73 { accessor => 'single',
edcecdbb 74 cascade_update => $default_cascade,
75 cascade_delete => $default_cascade,
07037f89 76 ($join_type ? ('join_type' => $join_type) : ()),
503536d5 77 %{$attrs || {}} });
07037f89 78 1;
22b15c96 79}
80
2339d6c7 81sub _validate_has_one_condition {
dc571b76 82 my ($class, $cond ) = @_;
83
84 return if $ENV{DBIC_DONT_VALIDATE_RELS};
85 return unless 'HASH' eq ref $cond;
86 foreach my $foreign_id ( keys %$cond ) {
87 my $self_id = $cond->{$foreign_id};
88
89 # we can ignore a bad $self_id because add_relationship handles this
90 # warning
91 return unless $self_id =~ /^self\.(.*)$/;
92 my $key = $1;
e705f529 93 $class->throw_exception("Defining rel on ${class} that includes '$key' but no such column defined here yet")
2339d6c7 94 unless $class->has_column($key);
dc571b76 95 my $column_info = $class->column_info($key);
96 if ( $column_info->{is_nullable} ) {
416e0bc0 97 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.');
dc571b76 98 }
99 }
100}
101
22b15c96 1021;