html-formhandler-model-dbic icon indicating copy to clipboard operation
html-formhandler-model-dbic copied to clipboard

Uniqueness checks should use actual column names, not accessors

Open robrwo opened this issue 5 years ago • 0 comments

Sometimes a DBIC schema uses an alternative accessor instead of the actual column name, especially when working with legacy databases, e.g.

__PACKAGE__->add_columns(
  "uco" =>  {
    accessor    => "username",
    data_type   => "text",
    is_nullable => 0,
    original    => { data_type => "varchar" },
  },
  ...
);

However, search conditions must use the actual column name, not the accessor. So the following will not work:

$rs->search( { username => { '!=', $row->username }, ... );

But HFH::Model::DBIIC uses the accessor instead of the actual name (from line 260):

my $accessor = $field->accessor;
 
my $count = $rs->search( { $accessor => $value, @id_clause } )->count;

You can work around this


use List::Util 1.29 qw/ pairmap /;

# These aliases could probably be saved in an attribute

my %aliases = pairmap  { $b->{accessor} || $a => $a, $a => $a }
        %{ $self->resultset->result_source->columns_info };

my $accessor = $field->accessor;
my $column = $aliases{ $accessor } || $accessor;
 
my $count = $rs->search( { $column => $value, @id_clause } )->count;

robrwo avatar May 17 '19 11:05 robrwo