jsmith2012 has asked for the wisdom of the Perl Monks concerning the following question:

i cant get the join to work for the dbix class. below are my 2 tables - i have removed the unnecessary columns. I am trying to perform the SQL query: "select * from image_folder as f, image_description as d where f.name = 'japan' and f.id=d.folder_id". i have tried following the dbix documentation to no avail. any suggestions?

package WorldTravelBlog::Model::DB::ImageDescription; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("Core"); __PACKAGE__->table("image_description"); __PACKAGE__->add_columns( "id", { data_type => "INT", default_value => undef, is_nullable => 0, size + => 11 }, "folder_id", { data_type => "INT", default_value => undef, is_nullable => 0, size + => 11 }, "name", { data_type => "VARCHAR", default_value => undef, is_nullable => 0, size => 64, }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( "folder_id", "WorldTravelBlog::Model::DB::ImageFolder", { id => "folder_id" }, ); 1;
package WorldTravelBlog::Model::DB::ImageFolder; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("Core"); __PACKAGE__->table("image_folder"); __PACKAGE__->add_columns( "id", { data_type => "INT", default_value => undef, is_nullable => 0, size + => 11 }, "name", { data_type => "VARCHAR", default_value => undef, is_nullable => 0, size => 64, }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( "image_descriptions", "WorldTravelBlog::Model::DB::ImageDescription", { "foreign.folder_id" => "self.id" }, ); 1;

Replies are listed 'Best First'.
Re: DBIx join problem
by jrsimmon (Hermit) on Jul 30, 2009 at 18:43 UTC

    A couple thoughts:

    • Have you tested your sql statement with DBI and found that it worked (ie, how sure are you it's DBIx)
    • What errors were reported by DBIx when you attempted to prepare and/or execute the statement?

    FWIW, I have no trouble joining tables and creating charts based off a select statement using DBIx.

      below is the dbix query that i came up w/ from the documentation. The error i got was "No relationship DBIx::Class::ResultSet::count(): No such relationship image_folder". i have tested the sql and it works.
      my $rs = $schema->resultset('ImageDescription')->search( { 'image_folder.name' => 'japan' }, { join => { 'image_folder' => 'image_folder'} } );
Re: DBIx join problem
by mzedeler (Pilgrim) on Jul 30, 2009 at 08:00 UTC

    The two code snippets above only defines metadata for use to configure DBIx::Class. Where is the code where you try to query and fail?