[Dbix-class] foreigns keys, joins, am I doing something stupid?

Chisel Wright chisel at herlpacker.co.uk
Thu Jan 12 12:12:48 CET 2006


I hit upon a problem with tring to use information in a secondary table
(joined my a foreign key).
I'm sure I'm doing something really dumg, but after looking at this for
some time I can't see what it is.

Below, I've copied in my schema (copied from MST's DBIx tests, and
stuffed into postgres), my test script, and the output.

I guess it boils down to is my schema wrong, or am I calling the search
incorrectly?

  $cds = Nipple::Model::NippleDB::Cd->search(
    {
        'artist.name' => 'We Are Goth',
    }
  );

Can someone tell me where my thinko is?

Chisel

---------------------------------------------------------------------------

CREATE TABLE artist (
    artistid integer NOT NULL,
    name character varying
);

INSERT INTO artist VALUES (1, 'Caterwauler McCrae');
INSERT INTO artist VALUES (2, 'Random Boy Band');
INSERT INTO artist VALUES (3, 'We Are Goth');


CREATE TABLE cd (
    cdid integer NOT NULL,
    artist integer NOT NULL,
    title character varying,
    "year" character varying
);

INSERT INTO cd VALUES (1, 1, 'Spoonful of bees', '1999');
INSERT INTO cd VALUES (2, 1, 'Forkful of bees', '2001');
INSERT INTO cd VALUES (3, 1, 'Caterwaulin Blues', '1997');
INSERT INTO cd VALUES (4, 2, 'Generic Manufactured Singles', '2001');
INSERT INTO cd VALUES (5, 3, 'Come Be Depressed With Us', '1998');

---------------------------------------------------------------------------

nipple=> select * from artist; select * from cd;
 artistid |        name
----------+--------------------
        1 | Caterwauler McCrae
        2 | Random Boy Band
        3 | We Are Goth
(3 rows)

 cdid | artist |            title             | year
------+--------+------------------------------+------
    1 |      1 | Spoonful of bees             | 1999
    2 |      1 | Forkful of bees              | 2001
    3 |      1 | Caterwaulin Blues            | 1997
    4 |      2 | Generic Manufactured Singles | 2001
    5 |      3 | Come Be Depressed With Us    | 1998
(5 rows)

---------------------------------------------------------------------------

use Test::More tests => 4;
use_ok( Catalyst::Test, 'Nipple' );
use_ok('Nipple::Model::NippleDB::Cd');

my $cds;

# 1. get a cd by year
$cds = Nipple::Model::NippleDB::Cd->search(
    year => 1998,
);
is ($cds->count(), 1, 'One CD found for 1998');

# 2. get a cd by artist name
$cds = Nipple::Model::NippleDB::Cd->search(
    {
        'artist.name' => 'We Are Goth',
    }
);
is ($cds->count(), 1, 'One CD found for "We Are Goth"');

---------------------------------------------------------------------------

chisel at ferrari Nipple $ perl -Ilib t/model_NippleDB-Cd.t
1..4

** snip catalyst output **

ok 1 - use Catalyst::Test;
ok 2 - use Nipple::Model::NippleDB::Cd;
ok 3 - One CD found for 1998
NOTICE:  adding missing FROM-clause entry for table "artist"
not ok 4 - One CD found for "We Are Goth"
#   Failed test 'One CD found for "We Are Goth"'
#   in t/model_NippleDB-Cd.t at line 19.
#          got: '5'
#     expected: '1'

---------------------------------------------------------------------------
-- 
Chisel Wright
e: chisel at herlpacker.co.uk
w: http://www.herlpacker.co.uk/

This is not an automated signature. I type this in to the bottom of every
message. 



More information about the Dbix-class mailing list