[Dbix-class] Prefetch multiple many_to_many?

Matt S Trout dbix-class at trout.me.uk
Mon Nov 5 22:10:21 GMT 2007


On Mon, Nov 05, 2007 at 10:21:46PM +0100, Ben Staude wrote:
> Am Samstag 03 November 2007 21:56 schrieb Matt S Trout:
> > On Sat, Nov 03, 2007 at 06:23:57PM +0100, Ben Staude wrote:
> > > Hi,
> > >
> > > I have a setup like the many_to_many-example Actor/Role in the docs, but
> > > the Actor has another many_to_many relationshiop to, for example,
> > > languages:
> > >
> > > Actor->many_to_many(roles=> 'actor_roles', 'role');
> > > Actor->many_to_many(languages => 'actor_languages', 'language');
> > >
> > > When selecting an Actor, I'd like to prefetch his roles and languages.
> > > I'm not yet sure whether this really makes sense with respect to
> > > performance for my application, but at least I want to understand what's
> > > going wrong when trying to do so:
> > >
> > > # Get actors by ids
> > > my @actors = $schema->resultset('Actor')->search(
> > >                 { 'me.id' => { -in => \@actor_ids } },
> > > 		{ prefetch => { actor_roles => 'role',
> > > 		                actor_languages => 'language' } })->all;
> >
> > We can't currently prefetch more than one has_many rel at the same level.
> >
> > Sorry.
> 
> No problem, I don't depend on it. Could an appropriate error or warning 
> message be implemented?  I think it's quite dangerous because it doesn't 
> really break, but silently leads to wrong / unexpected results.

It normally throws an exception, albeit a not really clear one.
 
> What about the other issue I mentioned that, after prefetching only 
> actor_roles => 'role', 
> the statement
> 
> map { $_->title } $actor->roles
> 
> yields a new SELECT instead of using the prefetched data? I can circumvent 
> this via
> 
> map { $_->role->title } $actor->actor_roles

Your DBIC is old, I think.

That was fixed a while back (08000 I think, if not very soon after)

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list