[Dbix-class] How to count?...

Bruce J Keeler bruce at drangle.com
Mon Mar 24 21:03:25 GMT 2008


Alex Povolotsky wrote:
> Hello!
>
> I have a simple tree-like structure of galleries, albums and photos; 
> to count photos in all albums, I've tried to
>
> sub count_photos {
>    my $self = shift;
>    return $self->photos->search->count;
> }
>
> in Album class.
>
> However, this yields
>
> SELECT COUNT( * ) FROM album me LEFT JOIN photo photos ON ( photos.aid 
> = me.aid ) WHERE ( me.gid = ? ): '1'
>
> which apparently returns wrong result. Can I somehow
Is gid the gallery id?  If so, that query would count the number of 
photos in all albums in that gallery.  Have you mis-identified gid as 
the primary key of Album?
>
> select count(*) from  photo photos LEFT JOIN album me ON ( photos.aid 
> = me.aid ) WHERE ( me.gid = ? ) ?
>
> Or am I missing something quite SQL-trivial?
That should be equivalent to the first query.



More information about the DBIx-Class mailing list