# no join condition or just a column name
if (!ref $cond) {
$class->ensure_class_loaded($f_class);
- my %f_primaries = map { $_ => 1 } try { $f_class->result_source_instance->_pri_cols_or_die }
- catch {
- $class->throw_exception( "Can't infer join condition for '$rel' on ${class}: $_");
- };
- my ($pri, $too_many) = keys %f_primaries;
- $class->throw_exception(
- "Can't infer join condition for '$rel' on ${class}: "
- . "${f_class} has multiple primary keys"
- ) if $too_many;
+ my $pri = $f_class->result_source_instance->_single_pri_col_or_die;
+
+ my ($f_key, $guess);
+ if (defined $cond and length $cond) {
+ $f_key = $cond;
+ $guess = "caller specified foreign key '$f_key'";
+ }
+ else {
+ $f_key = $rel;
+ $guess = "using given relationship name '$rel' as foreign key column name";
+ }
- my $fk = defined $cond ? $cond : $rel;
$class->throw_exception(
- "Can't infer join condition for '$rel' on ${class}: "
- . "'$fk' is not a column of $class"
- ) unless $class->has_column($fk);
+ "No such column '$f_key' declared yet on ${class} ($guess)"
+ ) unless $class->has_column($f_key);
- $cond = { "foreign.${pri}" => "self.${fk}" };
+ $cond = { "foreign.${pri}" => "self.${f_key}" };
}
# explicit join condition
unless (ref $cond) {
$class->ensure_class_loaded($f_class);
- my ($pri, $too_many) = try { $class->result_source_instance->_pri_cols_or_die }
- catch {
- $class->throw_exception("Can't infer join condition for '$rel' on ${class}: $_");
- };
- $class->throw_exception(
- "has_many can only infer join for a single primary key; ".
- "${class} has more"
- ) if $too_many;
+ my $pri = $class->result_source_instance->_single_pri_col_or_die;
my ($f_key,$guess);
if (defined $cond && length $cond) {
} else {
$class =~ /([^\:]+)$/; # match is safe - $class can't be ''
$f_key = lc $1; # go ahead and guess; best we can do
- $guess = "using our class name '$class' as foreign key";
+ $guess = "using our class name '$class' as foreign key source";
}
my $f_class_loaded = try { $f_class->columns };
unless (ref $cond) {
$class->ensure_class_loaded($f_class);
- my $pri = $class->_get_primary_key;
-
- $class->throw_exception(
- "might_have/has_one needs a primary key to infer a join; ".
- "${class} has none"
- ) if !defined $pri && (!defined $cond || !length $cond);
+ my $pri = $class->result_source_instance->_single_pri_col_or_die;
my $f_class_loaded = try { $f_class->columns };
- my ($f_key,$too_many,$guess);
+ my ($f_key,$guess);
if (defined $cond && length $cond) {
$f_key = $cond;
$guess = "caller specified foreign key '$f_key'";
} elsif ($f_class_loaded && $f_class->has_column($rel)) {
$f_key = $rel;
- $guess = "using given relationship '$rel' for foreign key";
- } else {
- $f_key = $class->_get_primary_key($f_class);
+ $guess = "using given relationship name '$rel' as foreign key column name";
+ } elsif ($f_class_loaded and my $f_pri = try {
+ $f_class->result_source_instance->_single_pri_col_or_die
+ }) {
+ $f_key = $f_pri;
$guess = "using primary key of foreign class for foreign key";
}
+
$class->throw_exception(
"No such column '$f_key' on foreign class ${f_class} ($guess)"
) if $f_class_loaded && !$f_class->has_column($f_key);
+
$cond = { "foreign.${f_key}" => "self.${pri}" };
}
$class->_validate_has_one_condition($cond);
1;
}
-sub _get_primary_key {
- my ( $class, $target_class ) = @_;
- $target_class ||= $class;
- my ($pri, $too_many) = try { $target_class->result_source_instance->_pri_cols_or_die }
- catch {
- $class->throw_exception("Can't infer join condition on ${target_class}: $_");
- };
-
- $class->throw_exception(
- "might_have/has_one can only infer join for a single primary key; ".
- "${class} has more"
- ) if $too_many;
- return $pri;
-}
-
sub _validate_has_one_condition {
my ($class, $cond ) = @_;
return @pcols;
}
+# same as above but mandating single-column PK (used by relationship condition
+# inferrence)
+sub _single_pri_col_or_die {
+ my $self = shift;
+ my ($pri, @too_many) = $self->_pri_cols_or_die;
+
+ $self->throw_exception( sprintf(
+ "Operation requires a single-column primary key declared on '%s'",
+ $self->source_name || $self->result_class || $self->name || 'Unknown source...?',
+ )) if @too_many;
+ return $pri;
+}
+
+
=head2 sequence
Manually define the correct sequence for your table, to avoid the overhead