[Dbix-class] wishlist: "might_have_many"

Matt S Trout dbix-class at trout.me.uk
Thu Apr 13 19:57:03 CEST 2006


Mark Hedges wrote:
> On Thu, 13 Apr 2006, Matt S Trout wrote:
>> Mike Rylander wrote:
>>> On 4/12/06, Mark Hedges <hedges at ucsd.edu> wrote:
>>>> I found that if I do a prefetch on a Foo RS to its have_many
>>>> Bar, but it doesn't have any Bars, I don't get the Foo at all.
>>>>
>>>> This is similar to a problem where if I do a prefetch from one
>>>> to another with a 1:1 relationship, but the other doesn't exist,
>>>> it does not work if I defined with has_one but it does work if I
>>>> defined if might_have.
>>>>
>>>> However there isn't a fix for a 1:N relationship.  (Doesn't 1:N
>>>> imply that N could equal 0?)  Instead I have to remember where
>>>> this drop might occur and I get no object, and I can't prefetch.
>>>>
>>> Sounds like has_many should use LEFT JOIN ... [he says, sniping from
>>> the sidelines having not looked at the code]
>> It does :) -
> 
> Hrmm, you're right.  And when I just fetch that relationship it 
> works fine, I get the top-level entity.  It happened when I did this:
> 
>     Foo has_many Bar belongs_to Baz
> 
>     ... prefetch => { bar => 'baz' }
> 
> But no Bar showed up.  Then I didn't get anything at all.

Aha. that'll be because the second hop is an inner join. What needs doing is 
making DBIC produce

foo LEFT JOIN (bar JOIN baz ON ...) ON ...

instead of

foo LEFT JOIN bar ON ... JOIN baz ON ...

but I don't have the tuits right now. Patches welcome, or I'll try and get to 
it next time I dive into the join code.

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list