对于DBIC分组查询没有这种关系

时间:2019-02-25 19:34:52

标签: sql perl catalyst dbix-class

我尝试使用以下SQL查询对按类型分组的所有产品进行计数:

  select pt.id,
         pt.name,
         count(p.id) as n
    from prd.product_types as pt,
         prd.products as p
   where pt.id = p.type
group by pt.id,
         pt.name
order by pt.name

这两个表( prd.product_types prd.products )由这两个DBIC类映射:

package ki::Schema::Result::ProductTypes;

use strict;
use warnings;
use utf8;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;

extends 'DBIx::Class::Core';

__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "EncodedColumn");
__PACKAGE__->table("prd.product_types");
__PACKAGE__->add_columns(
  "id",
  {
    data_type => "uuid",
    default_value => \"uuid_generate_v4()",
    is_nullable => 0,
    size => 16,
  },
  "name",
  { data_type => "varchar", is_nullable => 0, size => 128 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("uk_product_types", ["name"]);
__PACKAGE__->has_many(
  "products",
  "ki::Schema::Result::Products",
  { "foreign.type" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->meta->make_immutable;

1;

package ki::Schema::Result::Products;

use strict;
use warnings;
use utf8;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;

extends 'DBIx::Class::Core';

__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "EncodedColumn");
__PACKAGE__->table("prd.products");
__PACKAGE__->add_columns(
  "id",
  {
    data_type => "uuid",
    default_value => \"uuid_generate_v4()",
    is_nullable => 0,
    size => 16,
  },
  "type",
  { data_type => "uuid", is_foreign_key => 1, is_nullable => 0, size  => 16 },
  "name",
  { data_type => "varchar", is_nullable => 0, size => 128 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("uk_products", ["name"]);
__PACKAGE__->belongs_to(
  "type",
  "ki::Schema::Result::ProductTypes",
  { id => "type" },
  { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->meta->make_immutable;

1;

当我在Catalyst控制器中设置联接时,会得到“没有这种关系”:

DBIx::Class::ResultSource::_resolve_join(): No such relationship prd.products on ProductTypes

结果集是这样:

$c->stash(product_types_rs => $c->model('DB_T::ProductTypes'));

连接看起来像这样:

    $c->stash(categories => [$c->stash->{product_types_rs}->search({}, {
        join     => [qw/ prd.products /],
        select   => [ { count => 'prd.products.id' } ],
        as       => [qw/ n /],
        group_by => [qw/ id /],
        order_by => 'name ASC'
    })]);

上面的查询是DBIC文档中对artist-cd查询的一对一复制。但是有些事情我无法理解,这是错误的。

返回一些新闻:

到目前为止唯一可行的方法是:

$c->stash(categories => [$c->stash->{product_types_rs}->search({}, 
{
    '+select' => [{ COUNT => 'products.id', -as => 'n'}],
    join => ['products'],
    group_by => 'me.id',
    order_by => { -desc => ' COUNT( products.id )' },
})])

及其SQL看起来像这样:

  SELECT me.id, me.name, COUNT( products.id ) AS n
    FROM prd.product_types me LEFT JOIN prd.products products 
                                     ON products.type = me.id
GROUP BY me.id
ORDER BY COUNT( products.id ) DESC

我测试了生成的SQL查询,并得到了想要的结果。还有一个问题。在我看来,我确实使用了

[% FOREACH category IN categories -%]
    <li>([% category.n %])</li>
[% END -%]

循环以迭代结果集。但是即使Dumper-> Dump()向我显示正确的数据,也不会显示 n 的值:

...
'_column_data' => {
    'name' => "XTorckAlsa",
    'n' => 1,
    'id' => '88b94b12-4169-4964-9022-3eebbc5c05c5'
},
...
'_column_data' => {
    'name' => "Pledicts11",
    'n' => 5,
    'id' => 'ebda7223-99e3-48d6-b662-c7d17d124787'
},

我仍然想念一些东西。

1 个答案:

答案 0 :(得分:1)

您已将关系命名为products,因此需要在join参数中使用。表名称永远不会在任何DBIC参数中使用,仅在ResultSource定义中使用。

在其中一个参数中,您也永远不会超过一个点。

建议使用columns而不是selectas,因为它不太容易出现用户错误。

order_by也支持非文字SQL语法

[{ -asc => 'columnname' }]