No such relationship for a DBIC grouped query

I tried to count all products grouped by their types, using this SQL query:

  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

These two tables (prd.product_types and prd.products) are mapped by these two DBIC classes:

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;

and

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;

When I set the join it in my Catalyst controller, I get the "No such relationship":

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

The result set is this:

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

and the join looks like this:

    $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'
    })]);

The query above is a one to one reproduction of the query artist-cd from the DBIC documentation. But there is something what I can not understand and it is wrong.

Back with some news:

the only way what worked up to now is this:

$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 )' },
})])

and its SQL looks like this:

  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

I tested the generated SQL query and I get the wanted result. There is a remaining issue. In my view, I do use this

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

loop to iterate the result set. But the value of n is not displayed even if the Dumper->Dump() displays me the correct datas:

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

I am still missing something.

Upvotes: 1

Views: 113

Answers (1)

Alexander Hartmaier
Alexander Hartmaier

Reputation: 2204

You named the relationship products so that's what you need to use in the join parameter. A table name is never used in any DBIC parameter, only in the ResultSource definition.

You also never have more than a single dot in one of the parameters.

It's recommended to use columns instead of select and as because it's less prone to user errors.

order_by supports non-literal SQL syntax as well which is

[{ -asc => 'columnname' }]

Upvotes: 1

Related Questions