Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniqueness checks should use actual column names, not accessors #18

Open
robrwo opened this issue May 17, 2019 · 0 comments · May be fixed by #19
Open

Uniqueness checks should use actual column names, not accessors #18

robrwo opened this issue May 17, 2019 · 0 comments · May be fixed by #19

Comments

@robrwo
Copy link

robrwo commented May 17, 2019

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 added a commit to robrwo/html-formhandler-model-dbic that referenced this issue May 17, 2019
This fixes gshank#18 by using the column name instead of the accessor
in the search condition.
@robrwo robrwo linked a pull request May 17, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant