Grokbase
Topics Posts Groups | in
x
[ help ]

[Dbix-class] Nested joins + column subsets

View TopicPrint | Flat  Thread  Threaded
1) Mark Blythe In trying what seemed like a simple operation today, I've either discovered a bug or documentation...
| +1 vote (Anchor)
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
In trying what seemed like a simple operation today, I've either
discovered a bug or documentation deficiency.  For the sake of
brevity, I won't show full class definitions here, but let's say I
have 3 simple table classes: A, B, C.  They each have a single primary
key (a_id, b_id, c_id) with straightforward "belongs to" relationships
between them:

A = (belongs to) => B = (belongs to) => C

A simple nested prefetch works fine:

$schema->resultset('A')->find($a_id, {
   prefetch => { b => 'c' }
});

However, if either B or C has a big blob or text column that I don't
want to load, I may want to specify a subset of the columns.  Thus, I
tried the following:

$schema->resultset('A')->find($a_id, {
columns => [ 'me.a_id', 'me.b_id', 'b.b_id', 'b.c_id', 'c.c_id', 'c.title' ],
   join => { b => 'c' }
});

I was careful to specify the keys on both sides of the relationships
just to be safe.  This query does *not* work.  It dies with the error:

No such relationship "c"

I tried using 'select' rather than 'columns', but the same error
occurred.  After much debugging, I discovered that the resultset's
$self->{_attrs}{as} was being built differently than with the
prefetch.  It showed the 'b' and 'c' relationships on the same level
rather than nested, so later on when it died, it was looking for a "c"
relationship in the A class, which of course there isn't.

I was able to fix the query by adding the "as" attribute and nesting
the table prefixes there:

$schema->resultset('A')->find($a_id, {
select => [ 'me.a_id', 'me.b_id', 'b.b_id', 'b.c_id', 'c.c_id', 'c.title' ],
   as => [ 'a_id', 'b_id', 'b.b_id', 'b.c_id', 'b.c.c_id', 'b.c.title' ],
   join => { b => 'c' }
});

If this is expected and necessary, I didn't come across it in the
attribute docs under DBIx::Class::Resultset or in
DBIx::Class::Manual::Cookbook.

So is this a bug or a feature?  BTW, I'm using v.08008

Thanks,
Mark
2) Matt S Trout Feature. Both 'a' and 'b' could have a 'c' rel. This is how DBIC disambiguates. It's not documented...
| +1 vote (Anchor)
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
On Tue, Nov 20, 2007 at 03:58:05PM -0800, Mark Blythe wrote:
> In trying what seemed like a simple operation today, I've either
> discovered a bug or documentation deficiency. For the sake of
> brevity, I won't show full class definitions here, but let's say I
> have 3 simple table classes: A, B, C. They each have a single primary
> key (a_id, b_id, c_id) with straightforward "belongs to" relationships
> between them:
>
> A = (belongs to) => B = (belongs to) => C
>
> A simple nested prefetch works fine:
>
> $schema->resultset('A')->find($a_id, {
>    prefetch => { b => 'c' }
> });
>
> However, if either B or C has a big blob or text column that I don't
> want to load, I may want to specify a subset of the columns. Thus, I
> tried the following:
>
> $schema->resultset('A')->find($a_id, {
> columns => [ 'me.a_id', 'me.b_id', 'b.b_id', 'b.c_id', 'c.c_id', 'c.title' ],
>    join => { b => 'c' }
> });
>
> I was careful to specify the keys on both sides of the relationships
> just to be safe. This query does *not* work. It dies with the error:
>
> No such relationship "c"
>
> I tried using 'select' rather than 'columns', but the same error
> occurred. After much debugging, I discovered that the resultset's
> $self->{_attrs}{as} was being built differently than with the
> prefetch. It showed the 'b' and 'c' relationships on the same level
> rather than nested, so later on when it died, it was looking for a "c"
> relationship in the A class, which of course there isn't.
>
> I was able to fix the query by adding the "as" attribute and nesting
> the table prefixes there:
>
> $schema->resultset('A')->find($a_id, {
> select => [ 'me.a_id', 'me.b_id', 'b.b_id', 'b.c_id', 'c.c_id', 'c.title' ],
> as => [ 'a_id', 'b_id', 'b.b_id', 'b.c_id', 'b.c.c_id', 'b.c.title' ],
>    join => { b => 'c' }
> });
>
> If this is expected and necessary, I didn't come across it in the
> attribute docs under DBIx::Class::Resultset or in
> DBIx::Class::Manual::Cookbook.
>
> So is this a bug or a feature? BTW, I'm using v.08008

Feature. Both 'a' and 'b' could have a 'c' rel. This is how DBIC
disambiguates.

It's not documented because I don't think the people writing the docs did
this sort of selective prefetch; fancy whipping up a POD patch for us? :)

--
      Matt S Trout       Catalyst and DBIx::Class consulting and support -
Technical Director http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Christmas fun in collectable card game form -
http://www.shadowcat.co.uk/resources/2007_trading/

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org
3) Mark Blythe Thanks for clearing that up. I've written a POD patch (attached). I haven't submitted one of these...
paperclip | +1 vote (Anchor)
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
Thanks for clearing that up.  I've written a POD patch (attached).

I haven't submitted one of these before, so I wasn't sure exactly what
version of the file to prepare it against.  This one was prepped for
http://dev.catalyst.perl.org/repos/bast/DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod,
which is currently at revision 3872.

Let me know if I need to re-do it for any reason.

On Nov 21, 2007 9:24 AM, Matt S Trout <dbix-class@trout.me.uk> wrote:
> > So is this a bug or a feature? BTW, I'm using v.08008
>
> Feature. Both 'a' and 'b' could have a 'c' rel. This is how DBIC
> disambiguates.
>
> It's not documented because I don't think the people writing the docs did
> this sort of selective prefetch; fancy whipping up a POD patch for us? :)
>
> --
> Matt S Trout Catalyst and DBIx::Class consulting and support -
> Technical Director http://www.shadowcat.co.uk/catalyst/
> Shadowcat Systems Ltd. Christmas fun in collectable card game form -
> http://www.shadowcat.co.uk/resources/2007_trading/
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org _______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org

Attachment: multi-join-select-cookbook.patch
4) Matt S Trout Any chance you could patch the 'as' docs in ResultSet as well?
| +1 vote (Anchor)
[ Profile | Reply to group ] [ Flat  Thread  Threaded ]
On Wed, Nov 21, 2007 at 11:50:51AM -0800, Mark Blythe wrote:
> Thanks for clearing that up. I've written a POD patch (attached).
>
> I haven't submitted one of these before, so I wasn't sure exactly what
> version of the file to prepare it against. This one was prepped for
> http://dev.catalyst.perl.org/repos/bast/DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod,
> which is currently at revision 3872.
>
> Let me know if I need to re-do it for any reason.

Any chance you could patch the 'as' docs in ResultSet as well?

--
      Matt S Trout       Catalyst and DBIx::Class consulting and support -
Technical Director http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Christmas fun in collectable card game form -
http://www.shadowcat.co.uk/resources/2007_trading/

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org
spacer
View TopicPrint | Flat  Thread  Threaded
Home > Groups > dbix-class@lists.scsys.co.uk > [Dbix-class] Nested joins + column subsets (4 posts)