[Dbix-class] Example confusion -- my own

Alex Beamish talexb at gmail.com
Thu Jun 22 23:18:19 CEST 2006


On 6/22/06, Justin Guenther <jguenther at gmail.com> wrote:
>
> On 6/22/06, Alex Beamish <talexb at gmail.com> wrote:
>
> > Here's the first one that has me stumped ..
> >
> >   #     SELECT me.cdid, me.artist, me.title
> >   #       FROM cd me
> >   #  LEFT JOIN track tracks ON ( tracks.cd = me.cdid )
> >    #      WHERE ( tracks.title = ? )
> >   #  (`Stan')
> >
> >   sub get_cd_by_track {
> >     my $tracktitle = shift;
> >     print "get_cd_by_track($tracktitle):\n";
> >     my $rs = $schema->resultset('Cd')->search(
> >       {
> >         'tracks.title' => $tracktitle
> >       },
> >       {
> >         join     => [qw/ tracks /],
> >       }
> >     );
> >     my $cd = $rs->first;
> >     print $cd->title . "\n\n";
> >    }
> >
> > .. now we're joining on tracks instead of track, and I don't understand
> why.
> > I tried to understand this on IRC and I guess either didn't make myself
> > understood, or I'm thicker than two bricks. Note that the alias makes
> this
> > work .. tracks is an alias for track. When I tried changing tracks back
> to
> > track in the two places they appear, I got an error. Why does tracks
> work
> > and track does not?
>
> This is because the alias names in DBIx::Class are either (1) `me',
> for the current table, or (2) the _relationship_ name (or if more than
> 1 occurrence of the same rel occurs in the query, the following ones
> are named $rel_2 etc.)
>
> So join => 'tracks' doesn't mean join to the `tracks' table, it means
> join to the `tracks' relationship, which happens to exist on the
> `track' table. Just like in the first example you gave `cd' didn't
> refer to a table, it referred to a relationship that just so happened
> to be named the same as the table.


So does DBIx::Class drop the trailing 's' and look for the first
relationship in the other table?

The problem I'm having is that 'tracks' is in the Perl code, but 'tracks'
doesn't appear anywhere in the database, just 'track'. From the database:

CREATE TABLE track (
    trackid INTEGER PRIMARY KEY,
    cd INTEGER NOT NULL REFERENCES cd(cdid),
    title TEXT NOT NULL
  );

CREATE TABLE cd (
    cdid INTEGER PRIMARY KEY,
    artist INTEGER NOT NULL REFERENCES artist(artistid),
    title TEXT NOT NULL
  );

I really appreciate the help.

-- 
Alex Beamish
Toronto, Ontario
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/dbix-class/attachments/20060622/93b617bf/attachment.htm 


More information about the Dbix-class mailing list