stepamil has asked for the wisdom of the Perl Monks concerning the following question:
Hi PerlMonks,
I'm using DBIx::Class 0.082820 on Perl 5.10.1 and I have a situation when I'm trying to do count on table which is prefetched to another. Here's an example:
my $count = Core::Models->resultset('Shipment')->search( undef, { prefetch => 'rma' } )->count();
This creates this query:
SELECT COUNT( * ) FROM (SELECT me.id FROM shipment me GROUP BY me.id) meAnd logically it should be this:
SELECT COUNT( * ) FROM shipment me GROUP BY me.idMaybe I'm missing something. The real usecase is very complicated, but I've simplified it.
Can you please help?
Update (I've added the real models):
package Models::Result::Shipment; use strict; use base 'DBIx::Class::Core'; __PACKAGE__->table("shipment"); __PACKAGE__->add_columns( "id", { accesor => 'id', data_type => "integer", extra => { unsigned => 1 }, is_nullable => 0, is_auto_increment => 1 }, "ref", { accesor => 'reference', data_type => "varchar", is_nullable => 1, size => 255 }, "api_merchant_id", { data_type => "integer", extra => { unsigned => 1 }, is_nullable => 0, default_value => 1 }, # ... lot of other fields bellow ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( "rma", "Models::Result::RMA", { "foreign.shipment_reference" => "self.ref", "foreign.merchant_id" => "self.merchant_id", }, { cascade_delete => 0 } ); 1; package Models::Result::RMA; use strict; use base 'DBIx::Class::Core'; __PACKAGE__->table('api_rma'); __PACKAGE__->add_columns( 'id' => { data_type => 'integer', is_nullable => +0, extra => { unsigned => 1 } }, 'shipment_reference' => { data_type => 'varchar', is_nullable => +0, size => 255 }, 'merchant_id' => { data_type => "integer", extra => { unsigned + => 1 }, is_nullable => 0 }, # ... lot of other fields bellow ); __PACKAGE__->set_primary_key('id'); 1;
UPDATE 2: Here's what I have for now
$count = Core::Models->resultset('Shipment')->search( undef, { prefetch => 'rma', select => { count => 'DISTINCT(me.id)' }, as => [ 'total_count' ], } )->get_column('total_count')->first();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBIx::Class - Count with prefetch creates nested query
by Myrddin Wyllt (Hermit) on Jul 06, 2015 at 18:09 UTC | |
by stepamil (Acolyte) on Jul 07, 2015 at 07:30 UTC | |
|
Re: DBIx::Class - Count with prefetch creates nested query
by 1nickt (Canon) on Jul 06, 2015 at 11:50 UTC | |
by stepamil (Acolyte) on Jul 06, 2015 at 12:18 UTC | |
by 1nickt (Canon) on Jul 06, 2015 at 14:46 UTC |