Consolidate single-pk checks from relationship inferrence codepaths
[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) {
fd4df975 26 $class->ensure_class_loaded($f_class);
e36de82e 27
0b0743af 28 my $pri = $class->result_source_instance->_single_pri_col_or_die;
e36de82e 29
9780718f 30 my $f_class_loaded = try { $f_class->columns };
0b0743af 31 my ($f_key,$guess);
dcf8330b 32 if (defined $cond && length $cond) {
99be059e 33 $f_key = $cond;
dcf8330b 34 $guess = "caller specified foreign key '$f_key'";
103647d5 35 } elsif ($f_class_loaded && $f_class->has_column($rel)) {
07037f89 36 $f_key = $rel;
0b0743af 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
40 }) {
41 $f_key = $f_pri;
dcf8330b 42 $guess = "using primary key of foreign class for foreign key";
22b15c96 43 }
0b0743af 44
fd4df975 45 $class->throw_exception(
e705f529 46 "No such column '$f_key' on foreign class ${f_class} ($guess)"
fd4df975 47 ) if $f_class_loaded && !$f_class->has_column($f_key);
0b0743af 48
503536d5 49 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 50 }
2339d6c7 51 $class->_validate_has_one_condition($cond);
edcecdbb 52
53 my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
54
07037f89 55 $class->add_relationship($rel, $f_class,
56 $cond,
503536d5 57 { accessor => 'single',
edcecdbb 58 cascade_update => $default_cascade,
59 cascade_delete => $default_cascade,
07037f89 60 ($join_type ? ('join_type' => $join_type) : ()),
503536d5 61 %{$attrs || {}} });
07037f89 62 1;
22b15c96 63}
64
2339d6c7 65sub _validate_has_one_condition {
dc571b76 66 my ($class, $cond ) = @_;
67
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};
72
73 # we can ignore a bad $self_id because add_relationship handles this
74 # warning
75 return unless $self_id =~ /^self\.(.*)$/;
76 my $key = $1;
e705f529 77 $class->throw_exception("Defining rel on ${class} that includes '$key' but no such column defined here yet")
2339d6c7 78 unless $class->has_column($key);
dc571b76 79 my $column_info = $class->column_info($key);
80 if ( $column_info->{is_nullable} ) {
416e0bc0 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.');
dc571b76 82 }
83 }
84}
85
22b15c96 861;