M
M
MrKrot2016-11-19 00:48:26
Perl
MrKrot, 2016-11-19 00:48:26

How to write a request in DBIx::class to get information on all known users who have commented on their posts at least once?

I have 3 tables:

__PACKAGE__->add_columns(
  "id",
  {
    data_type         => "integer",
    is_auto_increment => 1,
    is_nullable       => 0,
    sequence          => "user_ids",
  },
  "name",  { data_type => "char", is_nullable => 1, size => 64 },
  "rating",  { data_type => "char", is_nullable => 1, size => 10 },
  "karma",  { data_type => "char", is_nullable => 1, size => 8 },
);
__PACKAGE__->has_many(
  "commenters",
  "My::Schema::Result::Commenter",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->has_many(
  "posts",
  "My::Schema::Result::Post",
  { "foreign.author" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

__PACKAGE__->add_columns(
  "id",
  {
    data_type         => "integer",
    is_auto_increment => 1,
    is_nullable       => 0,
    sequence          => "user_ids",
  },
  "author",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
  "theme",
  { data_type => "char", is_nullable => 1, size => 100 },
  "post_id",
  { data_type => "integer", is_nullable => 1 },
  "rating",
  { data_type => "char", is_nullable => 1, size => 10 },
  "views",
  { data_type => "char", is_nullable => 1, size => 10 },
  "stars",
  { data_type => "char", is_nullable => 1, size => 10 },
);
__PACKAGE__->has_many(
  "commenters",
  "My::Schema::Result::Commenter",
  { "foreign.post_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->belongs_to(
  "author",
  "My::Schema::Result::User",
  { id => "author" },
  {
    is_deferrable => 0,
    join_type     => "LEFT",
    on_delete     => "NO ACTION",
    on_update     => "NO ACTION",
  },
);

__PACKAGE__->add_columns(
  "user_id",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
  "post_id",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->belongs_to(
  "post",
  "My::Schema::Result::Post",
  { id => "post_id" },
  {
    is_deferrable => 0,
    join_type     => "LEFT",
    on_delete     => "NO ACTION",
    on_update     => "NO ACTION",
  },
);
__PACKAGE__->belongs_to(
  "user",
  "My::Schema::Result::User",
  { id => "user_id" },
  {
    is_deferrable => 0,
    join_type     => "LEFT",
    on_delete     => "NO ACTION",
    on_update     => "NO ACTION",
  },
);

Help me write a request for information on all known users who have commented on their posts at least once.
Tried:
my @commenters = $schema->resultset('Commenter')->search(
    { 'user_id' => 'post.author' }, { join => 'post' }
  )->search_related('user');

However, I get the error:
DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::Pg::st execute failed: ERROR: syntax error at or near "."
LINE 1: SELECT user.id, user.name, user.rating, user.karma FROM comm...
^ [for Statement "SELECT user.id, user.name, user.rating, user.karma FROM commenters me JOIN users user ON user.id = me.user_id WHERE ( user_id = ? )" with ParamValues: 1='post.author']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dionys, 2016-11-24
@dionys

This is how { 'user_id' => 'post.author' }field names cannot be compared. DBIC in this case considers that data is on the right, generates a condition user_id = ?and tries to substitute text for comparison with a numeric field.
The correct comparison should look like this: { 'me.user_id' => { -ident => 'post.author' } }.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question