[Dbix-class] Unknown column in 'group statement'
Peter Rabbitson
rabbit+dbic at rabbit.us
Thu Jul 23 21:45:02 GMT 2009
On Thu, Jul 23, 2009 at 04:19:01PM +0100, Anthony Gladdish wrote:
> Hi,
>
> Using:
> Perl 5.10.
> DBIC 0.08108.
>
> 1. My result source tables with relationships are:
>
> Event.pm:
> __PACKAGE__->set_primary_key('id');
> __PACKAGE__->belongs_to( 'leader' => 'Trainer' ) ;
> __PACKAGE__->has_many( 'assistingtrainers' => 'AssistingTrainer', 'event' );
> __PACKAGE__->many_to_many('assistants' => 'assistingtrainers', 'trainers' );
>
> AssistingTrainer.pm:
> __PACKAGE__->set_primary_key('event','trainer');
> __PACKAGE__->belongs_to( 'events' => 'Event', 'event' );
> __PACKAGE__->belongs_to( 'trainers' => 'Trainer', 'trainer' );
>
> Trainer.pm:
> __PACKAGE__->set_primary_key('id');
>
> 2. My ResultSet::Event.pm methods:
>
> sub goEvents {
> my $self = shift;
> my $where = {};
> $where->{status} = { '&' => 2 };
> return $self->search($where);
> }
>
> sub events_during_lookback_period {
> my $self = shift;
> my $default_lookback_period = DateTime::Duration->new( years => 3 );
> my $lookback_period = shift || $default_lookback_period;
> my $earliest_date = DateTime->now() - $lookback_period;
> return $self->goEvents()->search_rs(
> {
> -and => [
> start => { '<', DateTime->now() },
> start => { '>', $earliest_date },
> ],
> }
> );
> }
>
> sub distinct_assistants {
> my $self = shift;
> my $assistants = [];
> my $rs = $self->search_rs(
> undef,
> {
> columns => [ qw/me.id/ ],
> distinct => 1,
> prefetch => [ { 'assistingtrainers' => 'trainers' } ],
> join => [ { 'assistingtrainers' => 'trainers' } ],
> },
> );
> while ( my $e = $rs->next() ) {
> foreach my $at ( $e->assistingtrainers() ) {
> push( @$assistants, $at->trainers );
> }
> }
> return $assistants;
> }
>
This is a design lapse - we married distinct with group_by too early
in the code and what you are seeing is the fallout (i.e. distinct
applies to all columns not just the ones you are trying to select).
I will have to discuss this with the rest of the developers before
we solve it, but for the time being simply change
distinct => 1
to
group_by => [ qw/me.id/ ]
The prefetch will keep working as expected.
Cheers
More information about the DBIx-Class
mailing list