[Dbix-class] an extra undef result is coming while fetching result

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Feb 15 14:37:37 GMT 2012


On Wed, Feb 15, 2012 at 07:46:21PM +0530, tom wrote:
> I have a query as follows and it is giving an extra 'undef' result while  
> fetching the results.
>
>         my $item_cost_details_rs =  
> $self->item_db()->resultset('ItemDetails');
>         my @min_cost_items = $item_cost_details_rs->search(
>             {},
>                {
>
>                 join        => 'item_mappings',
>                 select      => [
>                     { min => 'item_mappings.item_mapping_cost' },
>                     'item_mappings.item'
>                 ],
>                 as          => [
>                     'cost',
>                     'item'
>                 ],
>                 group_by    => [
>                     'item_mappings.item'
>                 ]
>             }
>         );
>         foreach my $row (@min_cost_items){
>             my %details_item = $row->get_columns();
>             use Data::Dumper::Simple;
>             print STDERR Dumper(%details_item);
>         }
>
>
> There are only 2 results to display. But in foreach loop it s getting an  
> undef result in first looping. 2nd and 3rd looping give me the actual  
> result.
>
> %details_item = (
>                   'item' => undef,
>                   'cost' => undef
>                 );

How do you know there are 2 results to display? Did you run the actual query
as seen in DBIC_TRACE=1 through your console? Given the above construct I am
pretty sure item_mappings is a has_many, which means it is a left join, which
means you have several all nulls "rows" on the right side, which are
collapsed by the group to a single null/null/null... result, and returned to
you.

All speculation of course, the actual SQL will make it clearer.




More information about the DBIx-Class mailing list