[Dbix-class] question about change in prefetch behavior

Greg Hill gnhill at liquidweb.com
Wed Jul 25 20:20:38 GMT 2012


I'm currently researching what it would take to get us up-to-date with 
Perl and various CPAN modules, and I keep running into issues with 
prefetch in the updated DBIx::Class.  I couldn't find anything in the 
changelog that would explain what I'm seeing, so I figured I might as 
well finally join the list and ask.

This is the code in question:

     my $rs = $class->schema->resultset('products_to_categories')->search(
         $search,
         {
             prefetch => ['product_id','category_id'],
         }
     );

     my %by_category;
     while (my $row = $rs->next) {
         push @{$by_category{$row->category_id->category}}, 
$row->product_id->product_code;
     }


Perl dies because $row->category_id is undef:

Can't call method "category" on an undefined value at 
/home/gnhill/lwlibs/perl/LW/Product/Category.pm line 105.

The relationships being prefetched are:

__PACKAGE__->belongs_to(
   "product_id",
   "LW::Schema::BookKeep::products",
   { id => "product_id" },
);
__PACKAGE__->belongs_to(
   "category_id",
   "LW::Schema::BookKeep::product_categories",
   { id => "category_id" },
);

Ignoring the stupidity of us having generated our schemas using the 
column name as the relationship name (this was the default behavior in 
the schema generator at one point, and we had too much dependence on it 
to change it by the time I pointed out the insanity of it), any idea 
what's going wrong?  Or is that precisely the problem?

The other example I've found so far is:

     my $rs = $table->search(
         $search,
         {
             prefetch => 'product_region_prices', # this is a has_many
             cache    => 1,
         }
     );
     # passes $rs to a function that iterates through the whole thing
     $rs->next;
     while (my $product = $rs->next) {
     }

And I get this error:

"Unable to calculate a definitive collapse column set for product_data: 
fetch more unique non-nullable columns"

The relationship here is:

__PACKAGE__->has_many(
   "product_region_prices",
   "LW::Schema::BookKeep::product_region_prices",
   { "foreign.product_id" => "self.id" },
   { cascade_delete => 0 },
);

This code works without issue on version 0.08010 (I realize that it's 
many moons old).

Thanks for any help.

Greg




More information about the DBIx-Class mailing list