I want to create a paged content and i'm using DBIx for it. When I write a query without paging - it works fine. SQL without paging:
$c->stash->{routes} = [$c->model('QDB::Route')->search( { 'n_1.net_id' => $c->stash->{net_id}, }, { alias => 'rt', # alias columns in accordance with "from" from => [ { rt => 'vi_route' }, [ { n_1 => 'vi_nodes', -join_type => 'inner' }, { 'rt.route_beg' => 'n_1.node_id', 'rt.net_id' => 'n_1.net_id' } ], [ { n_2 => 'vi_nodes', -join_type => 'inner' }, { 'rt.route_end' => 'n_2.node_id', 'rt.net_id' => 'n_2.net_id' } ], ], 'select' => [ 'rt.route_beg', 'rt.route_end', \'n_1.node_name AS node_name_beg', \'n_2.node_name AS node_name_end', \'COUNT(rt.route_beg) as grp_count', ], 'as' => [ qw/ route_beg route_end node_name_beg node_nam +e_end route_grp_count / ], 'group_by' => [qw/ rt.route_beg rt.route_end node_name_beg nod +e_name_end /], }, )];
Equivalent SQL is normal:
SELECT rt.route_beg, rt.route_end, n_1.node_name AS node_name_beg, n_2.node_name AS node_name_end, COUNT(rt.route_beg) as grp_count FROM vi_route rt INNER JOIN vi_nodes n_1 ON ( rt.net_id = n_1.net_id AND rt.route_beg = n_1.node_id ) INNER JOIN vi_nodes n_2 ON ( rt.net_id = n_2.net_id AND rt.route_end = n_2.node_id ) WHERE ( n_1.net_id = ? ) GROUP BY rt.route_beg, rt.route_end, n_1.node_name, n_2.node_name ORDER BY node_name_beg: 'param_value'
But if I add few lines:
page => 1, # page to return (defaults to 1) rows => 10, # number of results per page
It becomes wrong and throw exception( Because sql query becomes bad. Equivalent SQL becomes bad:
SELECT * FROM ( SELECT TOP 10 * FROM ( SELECT TOP 10 rt.route_beg, rt.route_end, n_1.node_name AS no +de_name_beg, n_2.node_name AS node_name_end, COUNT(rt.route_beg) as g +rp_count FROM vi_route rt INNER JOIN vi_nodes n_1 ON ( rt.net_id = n_1.net_id AND rt.route_beg = n_1.node_id ) INNER JOI +N vi_nodes n_2 ON ( rt.net_id = n_2.net_id AND rt.route_end = n_2.node_id ) WHERE ( n_1.net_id = ? ) ORDER BY node_name_beg ASC ) AS foo #-- There must be GROUP BY CLAUSE but it's missed! --# ORDER BY node_name_beg DESC ) AS bar ORDER BY node_name_beg ASC : 'param_value'
DBIC missing group_by clause when generates sql for paged result. And sql server return error because aggregate function COUNT(rt.route_beg) not included in ORDER BY or GROUP BY clause. ======= If you think it was discussed in mail-lists - it's wrong! Or may be google search is broken?

In reply to dbix-class paging troubles - GROUP_BY by dreel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.