[Dbix-class] Prefetching resulset subqueries

Peter Rabbitson rabbit+dbic at rabbit.us
Sat Feb 14 11:57:35 GMT 2015

On 02/13/2015 02:12 PM, thilo.fester at googlemail.com wrote:
> ...
>          {
>              from  => [
>                  { 'me'    => 'artist' },
>                  [
>                      { 'tracklíst' => $track_list_rs->as_query },
>                      { 'me.artistid' => { -ident => 'tracklíst.artistid'
> } },
>                  ]
>              ],

(comment unrelated to your original question): Note that the above usage 
is unsupported (specifying a from structure by hand may or may not work 
this way). I literally can not tell if there are corner cases with the 
above or not without studying the source code for good 5 minutes. I 
understand there is no easy way to specify this stuff on the fly 
currently, read on for details why there is yet no defined API for that. 
In any case - make sure you test and comment this part of your code 
heavily - this *will* stop working in the distant future.

> Here I simply want to build a list of tracks per artist and connect it
> to the artist directly.
> For prefetching the tracklist, I use '+columns' and 'collapse', because
> I expected those two not to check for the accessor "tracklist" (but this
> seemed to be a wrong assumption).

The issue here isn't the accessors, but the relationship between "left 
and right side". collapse => 1 needs to know a lot of metadata about the 
'tracklist' to determine how to properly "fold" it when it sees 
repetitions of the same data (is it a duplication as a result of another 
join, or is it just multiple identical titles?). Currently this metadata 
can only be gleaned via 
$schema->source('Artist')->related_source('tracklist'). Since there is 
no such thing you get an exception (I need to improve the text of that).

In essence your best bet is to define a real relationship to "tracklist" 
with the correct subquery and everything, like in this example: 

If you are interested in the inner logic that collapse => 1 triggers, it 
can be found in this "double-recursion" function: 

If this reply isn't sufficient to answer your question - please ask further.

More information about the DBIx-Class mailing list