[Dbix-class] multiple has_many rels in prefetch not supported
	in	0.081?
    Moritz Onken 
    onken at houseofdesign.de
       
    Wed Aug 27 10:04:55 BST 2008
    
    
  
Am 26.08.2008 um 20:34 schrieb Matt S Trout:
> On Tue, Aug 26, 2008 at 03:03:11PM +0200, Moritz Onken wrote:
>> As I said I'm on it and writing some examples and tests.
>
> Ok, awesome. Please start a new thread when you've got that  
> together, I
> think this one was mostly the "resolving confusion" thread so the
> "figuring out what to fix" one can be its own thing :)
first test is attached. Couldn't find a test case where it returns
totally unrelated rows. The only problem is that it returns rows
multiple times.
############################################################
use strict;
use warnings;
use Test::More;
use lib qw(t/lib);
use DBICTest;
use Data::Dumper;
my $schema = DBICTest->init_schema();
my $orig_debug = $schema->storage->debug;
BEGIN {
     eval "use DBD::SQLite";
     plan $@
       ? ( skip_all => 'needs DBD::SQLite for testing' )
       : ( tests => 10 );
}
my $artist = $schema->resultset("Artist")->first;
$artist->cds->delete;
$artist->onekeys->delete;
$artist->create_related( "cds", { title => "cd1", year => "1984" } );
$artist->create_related( "cds", { title => "cd2", year => "1984" } );
$artist->create_related( "cds", { title => "cd3", year => "1984" } );
$artist->create_related( "onekeys", { cd => 1 } );
$artist->create_related( "onekeys", { cd => 2 } );
is( $artist->onekeys->count, 2, "count without prefetch is fine" );
is( $artist->cds->count, 2 );
my $search = {};
my $attr = { prefetch => [qw/cds onekeys/] };
$artist = $schema->resultset("Artist")->search( $search, $attr )->first;
is( $artist->onekeys->count, 2, "artist has 2 onekeys" )
   ;    # returns 6 which is 2*3 (see l. 29)
is( $artist->cds->count, 3, "artist has 3 cds" );
my @cds = sort ( map { $_->title } $artist->cds->all );
is_deeply( \@cds, [qw(cd1 cd2 cd3)], "no duplicates" );
my @onekeys = sort ( map { $_->cd } $artist->onekeys->all );
is_deeply( \@onekeys, [qw(1 2)] );    # @onekeys = (1,1,2,2);
$attr = { prefetch => [qw/onekeys cds/] };    # switch order
$artist =
   $schema->resultset("Artist")
   ->search( { artistid => $artist->artistid }, $attr )->first;
is( $artist->onekeys->count, 2, "artist has 2 onekeys" );
is( $artist->cds->count,     3, "artist has 3 cds" );
@cds = sort ( map { $_->title } $artist->cds->all );
is_deeply( \@cds, [qw(cd1 cd2 cd3)] )
   ;    # @onekeys = qw(cd1 cd1 cd2 cd2 cd3 cd3));
@onekeys = sort ( map { $_->cd } $artist->onekeys->all );
is_deeply( \@onekeys, [qw(1 2)] );
    
    
More information about the DBIx-Class
mailing list