Subject: | missing group by in final query |
Date: | Tue, 22 Sep 2015 20:27:05 +0200 |
To: | bug-DBIx-Class [...] rt.cpan.org |
From: | Martin Spevak <martin.spevak [...] hpe.com> |
Hello.
I found issue during resultset delete. Here is broken construction:
$radius->resultset('RadGroup')->search(undef, {
select => [ 'me.groupname' ],
join => [ qw/check_clear/ ],
group_by=> { -asc => 'me.groupname },
having => 'count(check_clear.groupname) = 0',
})->delete();
#I tried also group_by => 'me.groupname'
Here is constructed query with missing group by part (I got it using
DBIC_TRACE):
DELETE FROM radgroups WHERE ( groupname IN ( SELECT me.groupname FROM
radgroups me LEFT JOIN radgroupcheck check_clear ON
check_clear.groupname = me.groupname HAVING count(check_clear.groupname)
= 0 ) ):
with next error message:
message: DBI Exception: DBD::Pg::st execute failed: ERROR: column
"me.groupname" must appear in the GROUP BY clause or be used in an
aggregate function
LINE 1: ...LETE FROM radgroups WHERE ( groupname IN ( SELECT me.groupna...
^ [for
Statement "DELETE FROM radgroups WHERE ( groupname IN ( SELECT
me.groupname FROM radgroups me LEFT JOIN radgroupcheck check_clear ON
check_clear.groupname = me.groupname HAVING count(check_clear.groupname)
= 0 ) )"] at /usr/share/perl5/vendor_perl/DBIx/Class/Schema.pm line 1077
In RadGroup.pm I have:
__PACKAGE__->has_many(
"check_clear",
"HP_Infrasec::Models::Radius::Result::RadGroupCheck",
{ "foreign.groupname" => "self.groupname" },
{ cascade_copy => 0, cascade_delete => 0, join_type => 'LEFT' },
);
This is temporary working solution:
my $tmp_radgroup = $radius->resultset('RadGroup')->search(undef, {
select => [ 'me.groupname' ],
join => [ qw/check_clear/ ],
group_by=> 'me.groupname',
having => 'count(check_clear.groupname) = 0',
})->get_column('me.groupname');
$radius->resultset('RadGroup')->search({
'me.groupname' => { IN => $tmp_radgroup->as_query },
})->delete();
provides:
DELETE FROM radgroups WHERE ( groupname IN ( SELECT me.groupname FROM
radgroups me LEFT JOIN radgroupcheck check_clear ON
check_clear.groupname = me.groupname GROUP BY me.groupname HAVING
count(check_clear.groupname) = 0 ) ):
System information:
perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
DBIx version:
$VERSION = '0.082820';
--
*Martin (singer) Spevak*
HPES Software Development Engineer
HPES Network Management Solutions
Location: Galvaniho 7/A, Bratislava, Slovakia
Tel.: +421 2 5752 5390
Email: martin.spevak@hp.com