[Dbix-class] Q: prefetch has_many relation

Byron Young Byron.Young at riverbed.com
Fri Apr 9 07:07:12 GMT 2010


> -----Original Message-----
> From: Peter Rabbitson [mailto:rabbit+dbic at rabbit.us]
> Sent: Thursday, April 08, 2010 10:58 PM
> To: DBIx::Class user and developer list
> Subject: Re: [Dbix-class] Q: prefetch has_many relation
> 
> On Thu, Apr 08, 2010 at 02:43:37PM -0700, Byron Young wrote:
> > > -----Original Message-----
> > > From: Peter Rabbitson [mailto:rabbit+dbic at rabbit.us]
> > > Sent: Thursday, April 08, 2010 9:16 AM
> > > To: Class user and developer list
> > > Subject: Re: [Dbix-class] Q: prefetch has_many relation
> > >
> > > Bernhard Graf wrote:
> > > > Artist has_many CDs.
> > > >
> > [snip]
> > > > How do I select all CDs of the artists of $artist_rs with one
> query?
> > >
> > > my $new_rs = $artist_rs->search ({}, { prefetch => 'cds' });
> > >
> >
> > This brings up something I've been wondering about lately, which is
> how prefetch and join cause the resultset to behave when you prefetch
> or join on one or more has_many relationships.
> >
> > When I use prefetch on multiple has_many relationships, DBIC prints
> this warning:
> >
> > "DBIx::Class::ResultSet::all(): Prefetching multiple has_many rels
> jobs and queues at top level will explode the number of row objects
> retrievable via ->next or ->all. Use at your own risk."
> >
> > But I notice that $rs->all() actually returns the same number of rows
> as it does if I don't specify any prefetch or join (or if I specify a
> group_by => 'me.id').  If I specify a join instead of a prefetch it
> does actually explode the rows.  Here's output of a script I wrote to
> test this:
> 
> That's all nice, but see what does $rs->search_related('queues')->all
> returns,
> you'll be unpleasantly surprised :) Also note that it says "at top
> level". The
> warning is genuine, multiple has_many prefetch has been broken forever,
> there
> is a 95% complete fix for that, which I really hope to materialize
> before the
> end of the month. ETOOLITTLETIME.
> 

Ah yes, I see what you mean about the related resultsets.  Thanks for explaining.

Thanks,
byron



More information about the DBIx-Class mailing list